# Let's try it t1_start = time.time() print fibonacci(33) t1_elapsed = time.time() - t1_start print t1_elapsed # Let's try something harder t1_start = time.time() print fibonacci(100) t1_elapsed = time.time() - t1_start print t1_elapsed # Ok. Let's explain the trick in slow motion simcache.clear_keys() # Let's clean the cache # Let's define the real fibonacci computation function def fibonacci(n): if n < 2: return n print "Real fibonacci func, calling recursively to", fibonacci, n # Once the trick is done globals will contain a different function binded to 'fibonacci' return fibonacci(n - 1) + fibonacci(n - 2) print fibonacci print fibonacci(5) #=============================================================================== # Call graph of fibonacci for n=5
# Let's try it t1_start = time.time() print fibonacci(33) t1_elapsed = time.time() - t1_start print t1_elapsed # Let's try something harder t1_start = time.time() print fibonacci(100) t1_elapsed = time.time() - t1_start print t1_elapsed # Ok. Let's explain the trick in slow motion simcache.clear_keys() # Let's clear the cached keys first # Let's define the real fibonacci computation function def fibonacci(n): if n < 2: return n print "Real fibonacci func, calling recursively to", fibonacci, n # Once the trick is done globals will contain a different function binded to 'fibonacci' return fibonacci(n - 1) + fibonacci(n - 2) print fibonacci print fibonacci(5) #=============================================================================== # Call graph of fibonacci for n=5