text
stringlengths 0
828
|
---|
step 1 | 2.0528s | step1 = lambda iterable:(i for i in iterable if i%5==0) |
step 2 | 3.3039s | step3 = lambda iterable:sorted((1.0*i)/50 for i in iterable) |
step 3 | 3.1385s | step2 = lambda iterable:(i for i in iterable if i%8==3) |
step 4 | 3.1489s | step4 = lambda iterable:(float(float(float(float(i*3)))) for i in iterable) |
''' |
if callable(iterable): |
try: |
iter(iterable()) |
callable_base = True |
except: |
raise TypeError('time_pipeline needs the first argument to be an iterable or a function that produces an iterable.') |
else: |
try: |
iter(iterable) |
callable_base = False |
except: |
raise TypeError('time_pipeline needs the first argument to be an iterable or a function that produces an iterable.') |
# if iterable is not a function, load the whole thing |
# into a list so it can be ran over multiple times |
if not callable_base: |
iterable = tuple(iterable) |
# these store timestamps for time calculations |
durations = [] |
results = [] |
for i,_ in enumerate(steps): |
current_tasks = steps[:i+1] |
#print('testing',current_tasks) |
duration = 0.0 |
# run this test x number of times |
for t in range(100000): |
# build the generator |
test_generator = iter(iterable()) if callable_base else iter(iterable) |
# time its execution |
start = ts() |
for task in current_tasks: |
test_generator = task(test_generator) |
for i in current_tasks[-1](test_generator): |
pass |
duration += ts() - start |
durations.append(duration) |
if len(durations) == 1: |
results.append(durations[0]) |
#print(durations[0],durations[0]) |
else: |
results.append(durations[-1]-durations[-2]) |
#print(durations[-1]-durations[-2],durations[-1]) |
#print(results) |
#print(durations) |
assert sum(results) > 0 |
resultsum = sum(results) |
ratios = [i/resultsum for i in results] |
#print(ratios) |
for i in range(len(ratios)): |
try: |
s = getsource(steps[i]).splitlines()[0].strip() |
except: |
s = repr(steps[i]).strip() |
print('step {} | {:2.4f}s | {}'.format(i+1, durations[i], s))" |
4865,"def runs_per_second(generator, seconds=3): |
''' |
use this function as a profiler for both functions and generators |
to see how many iterations or cycles they can run per second |
Example usage for timing simple operations/functions: |
``` |
print(runs_per_second(lambda:1+2)) |
# 2074558 |
print(runs_per_second(lambda:1-2)) |
# 2048523 |
print(runs_per_second(lambda:1/2)) |
# 2075186 |
print(runs_per_second(lambda:1*2)) |
# 2101722 |
print(runs_per_second(lambda:1**2)) |
# 2104572 |
``` |
Example usage for timing iteration speed of generators: |
``` |
def counter(): |
c = 0 |
while 1: |
yield c |
c+=1 |
print(runs_per_second(counter())) |
# 1697328 |
print(runs_per_second((i for i in range(2000)))) |
# 1591301 |
``` |
''' |
assert isinstance(seconds, int), 'runs_per_second needs seconds to be an int, not {}'.format(repr(seconds)) |
assert seconds>0, 'runs_per_second needs seconds to be positive, not {}'.format(repr(seconds)) |
# if generator is a function, turn it into a generator for testing |
if callable(generator) and not any(i in ('next', '__next__', '__iter__') for i in dir(generator)): |
try: |
# get the output of the function |
output = generator() |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.