Last active
December 20, 2016 19:47
-
-
Save numberoverzero/4535761 to your computer and use it in GitHub Desktop.
Updated noop callback to correctly handle multiple args.
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
import time | |
def timer(): | |
start = time.time() | |
return lambda: time.time() - start | |
def timed(callback=None): | |
if callback is None: | |
callback = lambda *a, **kw: None | |
def wrapper(func): | |
def wrapped_call(*args, **kwargs): | |
_time = timer() | |
ret = func(*args, **kwargs) | |
delta = _time() | |
callback(delta) | |
return ret | |
return wrapped_call | |
return wrapper | |
### USAGE | |
def some_slow_function(n): | |
time.sleep(n) | |
def printer(delta): | |
print "Done in %s seconds." % delta | |
_time = timer() | |
some_slow_function(0.5) | |
delta = _time() | |
printer(delta) | |
### DECORATOR USAGE | |
def printer(delta): | |
print "Decorated Done in %s seconds." % delta | |
@timed(printer) | |
def another_slow_function(n): | |
time.sleep(n) | |
another_slow_function(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment