Skip to content

Instantly share code, notes, and snippets.

@headsrooms
Created November 15, 2018 16:55
Show Gist options
  • Save headsrooms/36fefdce586b897d813df3fe5eebac86 to your computer and use it in GitHub Desktop.
Save headsrooms/36fefdce586b897d813df3fe5eebac86 to your computer and use it in GitHub Desktop.
Timeit decorator asyncio compatible with repetitions and geometric mean result
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