Last active
August 23, 2016 09:29
-
-
Save makarchuk/3dd1b7e26a642d8177ce0a51a9402c64 to your computer and use it in GitHub Desktop.
decorators.py
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
import time | |
import random | |
class Profiler(): | |
def __init__(sel,f name): | |
self.name = name | |
self.count = 0 | |
def __call__(self, fn): | |
import time | |
def function(*args, **kwargs): | |
t = time.time() | |
fn(*args, **kwargs) | |
diff = time.time()-t | |
self.count+=1 | |
print("{}: CALLS: {}, TIME: {} sec.").format(self.name, self.count, diff) | |
return function | |
@Profiler('dumb') | |
def dumb(): | |
t = random.randint(1, 10)/100.0 | |
time.sleep(t) | |
print "Slept for {0} sec.".format(t) | |
@Profiler('dumb2') | |
def dumb2(): | |
t = random.randint(1, 10)/100.0 | |
time.sleep(t) | |
print "Slept for {0} sec.".format(t) | |
for x in range(100): | |
dumb() | |
if not x%3: | |
dumb2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment