fork download
  1. import time
  2. def timeit(method):
  3.  
  4. def timed(*args, **kw):
  5. ts = time.time()
  6. result = method(*args, **kw)
  7. te = time.time()
  8.  
  9. print ('%r (%r, %r) %2.2f sec' % (method.__name__, args, kw, te-ts))
  10. return result
  11.  
  12. return timed
  13.  
  14. def ceil(x):
  15. return x if x % 5 == 0 else (x // 5 + 1) * 5
  16.  
  17. def closest_mod_5(x):
  18. if x % 5 == 0:
  19. return x
  20. while True:
  21. x+=1
  22. if x % 5 == 0:
  23. return x
  24.  
  25. iterations = 1000000
  26.  
  27. @timeit
  28. def bad():
  29. for i in range(iterations):
  30. closest_mod_5(i)
  31.  
  32. @timeit
  33. def good():
  34. for i in range(iterations):
  35. ceil(i)
  36.  
  37. good()
  38. bad()
Success #stdin #stdout 0.52s 28384KB
stdin
Standard input is empty
stdout
'good' ((), {}) 0.19 sec
'bad' ((), {}) 0.31 sec