Created
October 31, 2018 04:45
-
-
Save NicHub/58f727e9bee77eb62d0e9fa2a7601970 to your computer and use it in GitHub Desktop.
Python timeit decorator
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""timeit decorator | |
Source: | |
https://stackoverflow.com/a/27737385/3057377 | |
Usage: | |
from timeitdeco import timeitdeco | |
@timeitdeco | |
def function_to_time(): | |
... | |
""" | |
from functools import wraps | |
from time import time | |
def timeitdeco(f): | |
@wraps(f) | |
def wrap(*args, **kw): | |
ts = time() | |
result = f(*args, **kw) | |
te = time() | |
print("func:%r args:[%r, %r] took: %2.4f sec" % \ | |
(f.__name__, args, kw, te-ts)) | |
return result | |
return wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment