fork download
  1. from time import clock
  2.  
  3. def a(n):
  4. return n + 1
  5.  
  6. def b1(loopcount):
  7. return sum([a(n) for n in range(loopcount)])
  8.  
  9. def b2(loopcount):
  10. def a(n):
  11. return n + 1
  12. return sum([a(n) for n in range(loopcount)])
  13.  
  14. def b3(loopcount):
  15. global a
  16. la = a
  17. return sum([la(n) for n in range(loopcount)])
  18.  
  19. powers = [5, 6, 7]
  20. b1times = []
  21. b2times = []
  22. b3times = []
  23. print " ", "".join(["{:^10d}".format(n) for n in powers])
  24. for i in range(5):
  25. for power in powers:
  26. t = clock()
  27. b1(10**power)
  28. b1times.append(clock() - t)
  29. for power in powers:
  30. t = clock()
  31. b2(10**power)
  32. b2times.append(clock() - t)
  33. for power in powers:
  34. t = clock()
  35. b3(10**power)
  36. b3times.append(clock() - t)
  37. print "b1:", "".join(["{:^10.5f}".format(n) for n in b1times])
  38. print "b2:", "".join(["{:^10.5f}".format(n) for n in b2times])
  39. print "b3:", "".join(["{:^10.5f}".format(n) for n in b3times])
  40. print ""
  41. b1times = []
  42. b2times = []
  43. b3times = []
Runtime error #stdin #stdout 2.51s 10896KB
stdin
Standard input is empty
stdout
        5         6         7