Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # recursive_vs_loops.py
- from timeit import timeit
- setup = """
- def fib(n):
- def _fib(prvprv, prv, c):
- if c < 1:
- return prvprv
- return _fib(prv, prvprv + prv, c - 1)
- return _fib(0, 1, n)
- """
- t = timeit(stmt='fib(35)',
- setup=setup,
- number=100000)
- exec(setup)
- print fib(35)
- print t
- setup = """
- def fib(n):
- a, b = 1, 1
- for _ in xrange(n - 1):
- a, b = b, a + b
- return a
- """
- t = timeit(stmt='fib(35)',
- setup=setup,
- number=100000)
- exec(setup)
- print fib(35)
- print t
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement