Understanding Caching in Python Fibonacci Series

  • Thread starter R.Harmon
  • Start date
  • Tags
    Python
In summary, the code to produce the Fibonacci series using python uses a function similar to that found on Wikibooks, but with a cache which speeds up the process.
  • #1
R.Harmon
24
0
Hey, I decided to start learning python so I downloaded it yesterday and decided to try and make a simple program to produce the Fibonacci series. I managed to do it using a function similar to that on wikibooks:

def fib(n):
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)

However, beneath that is a version with cache, where the code is:

def fib(n):
cache = {0: 0, 1: 1}
def xfib(n):
if n not in cache:
cache[n] = xfib(n-1) + xfib(n-2)
return cache[n]
assert n >= 0
return xfib(n)

I tested both these and found the 2nd is far quicker to produce the series, but I don't really understand why, mainly because I can't seem to work out what the line "cache = {0: 0, 1: 1}" is doing, so I was wondering if anyone knows what this piece of code means, and what its telling the program to do subsequently? Hope that's clear, thanks for any help!
 
Technology news on Phys.org
  • #2
It is creating a dictionary which stores the numbers '0' and '1' with the keys '0' and '1'
Dictionaries are an important paqrt of dynamic languages like python - think of them as mini databases, they let you store a value by giving it a key and then very quickly retreive it by just knowing the key. (They are called hashes in other languages)

In this case it just stores the fib sequence as it goes along and uses this cache to quickly calculate the next value by adding the previous two. The cache is a quick way of storing the last two values.
 
  • #3
Thats the initial conditions. Its saying that fib(0) is 0 and fib(1) is 1.

for example
a={1:"hello", 2:"bye"}
for example declares dict indexed by the numbers, so
a[1] returns "hello"
a[2] returns "bye"
 
  • #4
R.Harmon said:
I tested both these and found the 2nd is far quicker to produce the series, but I don't really understand why
Just count the number of addition operations required to do each.
 
  • #5
That definitely helps clear things up, thanks a lot everyone.
 

1. What is caching in Python Fibonacci Series?

Caching in Python Fibonacci Series is a technique used to store previously calculated values in memory, so that they can be quickly retrieved and used in subsequent calculations. This helps to improve the efficiency and speed of the program.

2. How does caching help in understanding the Fibonacci Series in Python?

Caching helps in understanding the Fibonacci Series in Python by reducing the number of calculations that need to be performed. By storing previously calculated values, the program can quickly retrieve them instead of repeating the same calculations, making the process more efficient.

3. What are the advantages of using caching in Python Fibonacci Series?

The main advantage of using caching in Python Fibonacci Series is improved performance. By reducing the number of calculations, the program runs faster and more efficiently. It also helps to save memory space by avoiding the need to store duplicate values.

4. Are there any potential drawbacks to using caching in Python Fibonacci Series?

One potential drawback of using caching in Python Fibonacci Series is the additional memory usage. Storing previously calculated values in memory can take up space and may not be feasible for larger or more complex calculations. Additionally, if the cache is not properly managed, it may lead to incorrect results.

5. How can I implement caching in my Python Fibonacci Series program?

To implement caching in a Python Fibonacci Series program, you can use a dictionary or a list to store the previously calculated values. Each time a new value is needed, the program can check the cache first before performing the calculation. You can also use the functools.lru_cache decorator which automatically caches the most recent function calls.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
11
Views
812
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
10
Views
728
Back
Top