Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function, division
- import time
- NUMBER_OF_RUNS = 4096
- NUMBER_OF_WARMUP_RUNS = 100//2
- def digits_genexp(number):
- return (int(digit) for digit in str(number))
- def digits_generator(number):
- while number:
- number, digit = divmod(number, 10)
- yield digit
- class Timed(object):
- def __init__(self):
- self._started = None
- self._ended = None
- def __enter__(self):
- self._started = time.time()
- return self
- def __exit__(self, *args):
- self._ended = time.time()
- return False
- @property
- def duration(self):
- return self._ended - self._started
- print('start warmup (', NUMBER_OF_WARMUP_RUNS, 'runs)')
- for _ in xrange(NUMBER_OF_WARMUP_RUNS):
- for i in xrange(NUMBER_OF_RUNS):
- list(digits_genexp(2**i))
- print('warmup stage 1 finished')
- for _ in xrange(NUMBER_OF_WARMUP_RUNS):
- for i in xrange(NUMBER_OF_RUNS):
- list(digits_generator(2**i))
- print('warmup finished')
- with Timed() as t:
- for i in xrange(NUMBER_OF_RUNS):
- list(digits_genexp(2**i))
- print('avg genexp duration:', t.duration/NUMBER_OF_RUNS)
- with Timed() as t:
- for i in xrange(NUMBER_OF_RUNS):
- list(digits_generator(2**i))
- print('avg generatorfunc duration:', t.duration/NUMBER_OF_RUNS)
- # pypy
- # # 0 warmup runs
- # avg genexp duration: 6.00620405748e-05
- # avg generatorfunc duration: 0.00206580612576
- #
- # # 50 warmup runs
- # avg genexp duration: 5.45402872376e-05
- # avg generatorfunc duration: 0.00207115866942
- #
- # # 100 warmup runs
- # avg genexp duration: 5.43720670976e-05
- # avg generatorfunc duration: 0.00206778023858
- #
- # cpython (no jit which needs warmup time
- # # 0 warmup runs
- # avg genexp duration: 0.000265139678959
- # avg generatorfunc duration: 0.000515451654792
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement