Created
February 22, 2019 00:36
-
-
Save allemangD/c2da7180720ae7442b5e93a1ac8423dc to your computer and use it in GitHub Desktop.
Utility function for timeit module - call a function with optional args and print statistics about timeit results
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def timedec(*args, func=None, repeat=5, number=1000000): | |
import timeit | |
from functools import partial | |
import statistics | |
def dec(f): | |
p = partial(f, *args) | |
pname = f"{f.__name__}({', '.join(map(str, args))})" | |
result = timeit.Timer(p).repeat(repeat, number) | |
mean = statistics.mean(result) | |
stdev = statistics.stdev(result) | |
max_ = max(result) | |
min_ = min(result) | |
sum_ = sum(result) | |
print( | |
f'timing {pname} with {number} iterations, {repeat} trials\n' | |
f' mean: {mean:.5f}s, stdev: {stdev:.5f}s\n' | |
f' min: {min_:.5f}s, max: {max_:.5f}s, sum: {sum_:.5f}s\n' | |
) | |
return f | |
if func is not None: | |
return dec(func) | |
return dec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment