Skip to content

Instantly share code, notes, and snippets.

@sputnikus
Created November 22, 2011 15:40
Show Gist options
  • Save sputnikus/1385958 to your computer and use it in GitHub Desktop.
Save sputnikus/1385958 to your computer and use it in GitHub Desktop.
Speed kills
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from math import sqrt
def fibonacci(n):
root5 = sqrt(5)
phi = 0.5 + root5 / 2
return int(0.5 + phi ** n / root5)
start = time.time()
print "fib(40) = %d" % (fibonacci(40))
print "Time elapsed = ", time.time() - start, "seconds"
λ ~/ python2 fib.py
fib(40) = 102334155
Time elapsed = 8.79764556885e-05 seconds
λ ~/ pypy fib.py
fib(40) = 102334155
Time elapsed = 0.000193119049072 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment