Skip to content

Instantly share code, notes, and snippets.

@neingeist
Created March 15, 2015 13:05
Show Gist options
  • Save neingeist/86d24ff38f7971f6dafc to your computer and use it in GitHub Desktop.
Save neingeist/86d24ff38f7971f6dafc to your computer and use it in GitHub Desktop.
a decorater example
from __future__ import division, print_function
from functools import wraps
import time
def timed(f):
"""Return a timed version of function f.
The returned function returns a tuple of (time, real return value)
:param f: function to time
"""
@wraps(f)
def wrapper(*args, **kwds):
time_start = time.time()
return_ = f(*args, **kwds)
time_end = time.time()
time_delta = time_end - time_start
return time_delta, return_
return wrapper
@timed
def an_example():
return "foo"
print(an_example())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment