fork(1) 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, base):
  15. return x if x % base == 0 else (x // base + 1) * base
  16.  
  17. from functools import partial
  18. ceil50 = partial(ceil, base=50)
  19.  
  20.  
  21. def closest_mod_50(x):
  22. if x % 50 == 0:
  23. return x
  24. while True:
  25. x+=1
  26. if x % 50 == 0:
  27. return x
  28.  
  29. iterations = 1000000
  30.  
  31.  
  32.  
  33. @timeit
  34. def bad():
  35. for i in range(iterations):
  36. closest_mod_50(i)
  37.  
  38. @timeit
  39. def good():
  40. for i in range(iterations):
  41. ceil50(i)
  42.  
  43. good()
  44. bad()
Success #stdin #stdout 2.44s 28384KB
stdin
Standard input is empty
stdout
'good' ((), {}) 0.39 sec
'bad' ((), {}) 2.03 sec