Skip to content

Instantly share code, notes, and snippets.

@NicHub
Created October 31, 2018 04:45
Show Gist options
  • Save NicHub/58f727e9bee77eb62d0e9fa2a7601970 to your computer and use it in GitHub Desktop.
Save NicHub/58f727e9bee77eb62d0e9fa2a7601970 to your computer and use it in GitHub Desktop.
Python timeit decorator
#!/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