Created
August 1, 2020 17:40
-
-
Save akarsh1995/65024ed157ab16f6bd7b71689a171d41 to your computer and use it in GitHub Desktop.
Timeit β³ decorator to measure the execution speed of the python function. Use anywhere you like...π
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
| import time | |
| def timeit(method): | |
| def timed(*args, **kw): | |
| ts = time.time() | |
| result = method(*args, **kw) | |
| te = time.time() | |
| if 'log_time' in kw: | |
| name = kw.get('log_name', method.__name__.upper()) | |
| kw['log_time'][name] = int((te - ts) * 1000) | |
| else: | |
| print(f'{method.__name__} func' | |
| f' took {((te - ts) * 1000):.2f} ms') | |
| return result | |
| return timed | |
| @timeit | |
| def sleeping(): | |
| time.sleep(5) | |
| if __name__ == '__main__': | |
| sleeping() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment