Last active
April 28, 2020 05:41
-
-
Save wiccy46/16ba6f5a50b39d59d61f2ef52a32241d to your computer and use it in GitHub Desktop.
[timer_decorator]Timeit decorator or class decorator#python
This file contains 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 '%r %2.2f ms' % \ | |
(method.__name__, (te - ts) * 1000) | |
return result | |
return timed | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment