Commit 9e583f02 authored by Stefan Behnel's avatar Stefan Behnel

new micro benchmark for generators

parent b2b1eeda
#!/usr/bin/python
# micro benchmarks for generators
COUNT = 10000
def count_to(N):
for i in range(N):
yield i
def round_robin(*iterators):
iterators = list(iterators)
to_drop = []
while iterators:
for i, it in enumerate(iterators):
try:
value = next(it)
except StopIteration:
to_drop.append(i)
else:
yield value
if to_drop:
for index in reversed(to_drop):
del iterators[index]
del to_drop[:]
def yield_from(*iterators):
for it in iterators:
yield from it
def bm_plain(N):
return count_to(COUNT * N)
def bm_round_robin(N):
return round_robin(*[ count_to(COUNT // i) for i in range(1,N+1) ])
def bm_yield_from(N):
return yield_from(count_to(N),
round_robin(*[ yield_from(count_to(COUNT // i))
for i in range(1,N+1) ]),
count_to(N))
def bm_yield_from_nested(N):
return yield_from(count_to(N),
yield_from(count_to(N),
round_robin(*[ yield_from(count_to(COUNT // i))
for i in range(1,N+1) ]),
count_to(N)),
count_to(N))
def time(fn, *args):
from time import time
begin = time()
result = list(fn(*args))
end = time()
return result, end-begin
def benchmark(N):
times = []
for _ in range(N):
result, t = time(bm_yield_from_nested, 10)
times.append(t)
return times
if __name__ == "__main__":
import optparse
parser = optparse.OptionParser(
usage="%prog [options]",
description=("Micro benchmarks for generators."))
import util
util.add_standard_options_to(parser)
options, args = parser.parse_args()
util.run_benchmark(options, options.num_runs, benchmark)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment