Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xhacker
Created November 18, 2014 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xhacker/fbf3d92d7b1fad9ddc09 to your computer and use it in GitHub Desktop.
Save xhacker/fbf3d92d7b1fad9ddc09 to your computer and use it in GitHub Desktop.
from timeit import timeit
setup = """
results = [0 for _ in xrange(100)]
results[1] = 1
results[2] = 1
def fib(n):
if results[n]:
return results[n]
results[n] = fib(n - 1) + fib(n - 2)
return results[n]
"""
timeit(stmt='fib(35)', setup=setup, number=1000000)
setup = """
def fib(n):
a, b = 1, 1
for _ in xrange(n - 1):
a, b = b, a + b
return a
"""
timeit(stmt='fib(35)', setup=setup, number=1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment