Understanding Caching in Python Fibonacci Series

  • Context: Python 
  • Thread starter Thread starter R.Harmon
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion centers around understanding caching in Python, specifically in the context of implementing the Fibonacci series. Participants explore the differences between a basic recursive implementation and a cached version, discussing the implications of using a cache for performance improvement.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant presents a basic recursive function for generating the Fibonacci series and a cached version, noting the performance difference.
  • Another participant explains that the line "cache = {0: 0, 1: 1}" creates a dictionary to store the initial Fibonacci values, which allows for quick retrieval.
  • A different participant emphasizes that the cache serves to store previously computed Fibonacci numbers, facilitating faster calculations for subsequent values.
  • One participant suggests counting the number of addition operations in each implementation as a way to understand the performance difference.

Areas of Agreement / Disagreement

Participants generally agree on the purpose of the cache in improving performance, but there is no explicit consensus on the deeper implications of caching or the best way to explain it.

Contextual Notes

Some participants provide examples and analogies to clarify the concept of dictionaries in Python, but there are no formal definitions or resolutions to the underlying complexities of caching mechanisms.

Who May Find This Useful

Individuals learning Python, particularly those interested in performance optimization and data structures like dictionaries.

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.
 

Similar threads

Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
55
Views
7K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 17 ·
Replies
17
Views
2K