fork(1) download
  1. import timeit
  2. import sys
  3. def fib(n):
  4. a,b = 0,1
  5. for _ in range(n):
  6. a,b = b,a+b
  7. return b
  8.  
  9.  
  10. setup = '''
  11. def gcd(a, b):
  12. """Calculate the Greatest Common Divisor of a and b.
  13.  
  14. Unless b==0, the result will have the same sign as b (so that when
  15. b is divided by it, the result comes out positive).
  16. """
  17. while b:
  18. a, b = b, a%b
  19. return a
  20. '''
  21. numbers = (fib(100),fib(101))
  22. print timeit.timeit('gcd(%d, %d)'%numbers, setup=setup,number=100000)
  23. print timeit.timeit('gcd(%d, %d)'%numbers, setup = 'gcd = lambda a,b : a if not b else gcd(b, a % b)',number=100000)
  24.  
Success #stdin #stdout 8.77s 4800KB
stdin
Standard input is empty
stdout
3.46678805351
5.29850101471