Last active
July 18, 2018 10:27
-
-
Save jancajthaml/4cf83e615b5aeb3e52c3f1df8357a3a9 to your computer and use it in GitHub Desktop.
measure execution of python block or method with single timeit
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import time | |
class timeit(): | |
def __init__(self, label): | |
import sys | |
import time | |
self.__time = time.time | |
self.__label = label | |
def __call__(self, f, *args, **kwargs): | |
self.__enter__() | |
result = f(*args, **kwargs) | |
self.__exit__() | |
return result | |
def __enter__(self): | |
self.ts = self.__time() | |
def __exit__(self, *args): | |
te = self.__time() | |
print('%s %2.2f ms' % (self.__label, (te - self.ts) * 1e3)) | |
if __name__ == "__main__": | |
@timeit('function_label') | |
def func(): | |
time.sleep(0.5) | |
for x in range(10): | |
with timeit('anonymous_block_{0}'.format(x)): | |
time.sleep(0.1) | |
with timeit('anonymous_block_noop'): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment