Created
November 22, 2011 15:40
-
-
Save sputnikus/1385958 to your computer and use it in GitHub Desktop.
Speed kills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
λ ~/ 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