Created
March 15, 2015 13:05
-
-
Save neingeist/86d24ff38f7971f6dafc to your computer and use it in GitHub Desktop.
a decorater example
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
| 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