Python Understanding Caching in Python Fibonacci Series

  • Thread starter Thread starter R.Harmon
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around learning Python through creating a Fibonacci series program. The user initially implemented a recursive function but found a second version with caching significantly faster. The caching mechanism uses a dictionary to store previously computed Fibonacci numbers, allowing for quicker retrieval and reducing the number of recursive calls. The line "cache = {0: 0, 1: 1}" initializes this dictionary with the base cases of the Fibonacci sequence. The use of dictionaries in Python is emphasized as a way to efficiently store and access values using keys. The conversation highlights the importance of understanding how caching optimizes performance by minimizing redundant calculations.
R.Harmon
Messages
23
Reaction score
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
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.
 
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"
 
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.
 
That definitely helps clear things up, thanks a lot everyone.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
15
Views
2K
Replies
1
Views
443
Replies
9
Views
3K
Replies
11
Views
1K
Replies
7
Views
5K
Back
Top