from __future__ import print_function
from time import time as ts
def time_pipeline(iterable, *steps):
''' this times the steps in a pipeline.
give it an iterable to test against
followed by the steps of the pipeline
seperated in individual functions.
'''
durations = []
for i in range(len(steps)):
i += 1
current_tasks = steps[:i]
print('testing',current_tasks)
duration = 0.0
# run this test 1000 times
for t in range(100000):
# build the generator
test_generator = iter(iterable)
for task in current_tasks:
test_generator = task(test_generator)
# time its execution
start = ts()
for i in test_generator:
pass
duration += ts() - start
durations.append(duration)
if len(durations) == 1:
print(durations[0],durations[0])
else:
print(durations[-1]-durations[-2],durations[-1])
l = list(range(50))
f1=lambda iterable:(i*2 for i in iterable)
f2=lambda iterable:(i*3 for i in iterable)
f3=lambda iterable:(i*4 for i in iterable)
time_pipeline(l, f1, f2, f3)