Skip to content

Instantly share code, notes, and snippets.

@abbeyj
Created September 5, 2009 05:35
Show Gist options
  • Save abbeyj/181302 to your computer and use it in GitHub Desktop.
Save abbeyj/181302 to your computer and use it in GitHub Desktop.
import time
import optparse
def fib(x):
if x==0 or x==1:
return 1
else:
return fib(x-2)+fib(x-1)
if __name__ == "__main__":
parser = optparse.OptionParser(
usage="%prog [options]",
description=("Test the performance of recursive Fibonacci."))
parser.add_option("-w", "--warmup", action="store_true", dest="warmup",
help="Warm up the function before testing")
parser.add_option("-o", "--optimize", action="store_true", dest="optimize",
help="Set co_optimization=2 before testing")
options, args = parser.parse_args()
if options.warmup:
print "Warming up ...",
for i in range(20000):
fib(0)
print "Done"
if options.optimize:
print "Optimizing ...",
fib.__code__.co_optimization = 2
fib.__code__.__use_llvm__ = True
fib(0)
print "Done"
if args:
n = int(args[0])
else:
n = 33
t = time.time()
fib(n)
print time.time() - t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment