Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. from __future__ import print_function, division
  2. import time
  3.  
  4. NUMBER_OF_RUNS = 4096
  5. NUMBER_OF_WARMUP_RUNS = 100//2
  6.  
  7. def digits_genexp(number):
  8. return (int(digit) for digit in str(number))
  9.  
  10. def digits_generator(number):
  11. while number:
  12. number, digit = divmod(number, 10)
  13. yield digit
  14.  
  15. class Timed(object):
  16. def __init__(self):
  17. self._started = None
  18. self._ended = None
  19.  
  20. def __enter__(self):
  21. self._started = time.time()
  22. return self
  23.  
  24. def __exit__(self, *args):
  25. self._ended = time.time()
  26. return False
  27.  
  28. @property
  29. def duration(self):
  30. return self._ended - self._started
  31.  
  32. print('start warmup (', NUMBER_OF_WARMUP_RUNS, 'runs)')
  33. for _ in xrange(NUMBER_OF_WARMUP_RUNS):
  34. for i in xrange(NUMBER_OF_RUNS):
  35. list(digits_genexp(2**i))
  36. print('warmup stage 1 finished')
  37.  
  38. for _ in xrange(NUMBER_OF_WARMUP_RUNS):
  39. for i in xrange(NUMBER_OF_RUNS):
  40. list(digits_generator(2**i))
  41. print('warmup finished')
  42.  
  43. with Timed() as t:
  44. for i in xrange(NUMBER_OF_RUNS):
  45. list(digits_genexp(2**i))
  46. print('avg genexp duration:', t.duration/NUMBER_OF_RUNS)
  47.  
  48. with Timed() as t:
  49. for i in xrange(NUMBER_OF_RUNS):
  50. list(digits_generator(2**i))
  51. print('avg generatorfunc duration:', t.duration/NUMBER_OF_RUNS)
  52.  
  53. # pypy
  54. # # 0 warmup runs
  55. # avg genexp duration: 6.00620405748e-05
  56. # avg generatorfunc duration: 0.00206580612576
  57. #
  58. # # 50 warmup runs
  59. # avg genexp duration: 5.45402872376e-05
  60. # avg generatorfunc duration: 0.00207115866942
  61. #
  62. # # 100 warmup runs
  63. # avg genexp duration: 5.43720670976e-05
  64. # avg generatorfunc duration: 0.00206778023858
  65. #
  66. # cpython (no jit which needs warmup time
  67. # # 0 warmup runs
  68. # avg genexp duration: 0.000265139678959
  69. # avg generatorfunc duration: 0.000515451654792
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement