Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment