Created
November 15, 2018 16:55
-
-
Save headsrooms/36fefdce586b897d813df3fe5eebac86 to your computer and use it in GitHub Desktop.
Timeit decorator asyncio compatible with repetitions and geometric mean result
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 timeit(n): | |
def timeit_decorator(func): | |
async def process(func, *args, **params): | |
if asyncio.iscoroutinefunction(func): | |
return await func(*args, **params) | |
else: | |
return func(*args, **params) | |
async def helper(*args, **params): | |
times = [] | |
for _ in range(n): | |
start = time() | |
await process(func, *args, **params) | |
times.append(time() - start) | |
dot_product = reduce((lambda x, y: x * y), times) | |
geometric_mean = dot_product ** (1 / n) | |
print('>>>', geometric_mean) | |
return helper | |
return timeit_decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment