-
Notifications
You must be signed in to change notification settings - Fork 9
/
timing.py
68 lines (55 loc) · 1.94 KB
/
timing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import time
class Timing(object):
def __init__(self):
self.timings = {}
self.col = self.__collector()
self.col.next() #coroutine syntax
def __collector(self):
while True:
(name, t) = (yield) #coroutine syntax
if name in self.timings:
self.timings[name]["timings"] += [t]
self.timings[name]["count"] += 1
self.timings[name]["total"] += t
else:
self.timings[name] = {} #if this entry doesn't exist yet
self.timings[name]["timings"] = [t]
self.timings[name]["count"] = 1
self.timings[name]["total"] = t
def __call__(self, func):
"""Turn the object into a decorator"""
def wrapper(*arg, **kwargs):
t1 = time.time() #start time
res = func(*arg, **kwargs) #call the originating function
t2 = time.time() #stop time
t = (t2-t1)*1000.0 #time in milliseconds
data = (func.__name__, t)
self.col.send(data) #collect the data
return res
return wrapper
def __str__(self):
s = "Timings:\n"
#print dir(self)
for key in self.timings.keys():
s += "%s | " % key
ts = self.timings[key]["timings"]
count = self.timings[key]["count"]
total = self.timings[key]["total"]
s += "average: %s | total: %s | count: %s\n" % (total / count, total, count)
return "%s" % s
if __name__ == "__main__":
timings = Timing()
@timings
def add(x,y):
for i in range(10000):
c = x + y
return c
@timings
def multiply(x,y):
for i in range(10000):
c = x * y
return c
for i in range(100):
add(3.,4.)
multiply(3., 4.)
print timings