-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchtime.py
More file actions
40 lines (32 loc) · 1011 Bytes
/
Copy pathbenchtime.py
File metadata and controls
40 lines (32 loc) · 1011 Bytes
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
#!/bin/env python3
import datetime
import json
statistics = {}
def benchmethod(func):
def method(*args, **kwargs):
name = func.__name__
start = datetime.datetime.now()
result = func(*args, **kwargs)
stop = datetime.datetime.now()
duration = stop - start
if name not in statistics:
statistics[name] = {
"docstring": func.__doc__,
"durations": [duration.total_seconds()],
}
else:
statistics[name]["durations"].append(duration.total_seconds())
return result
return method
def report():
res = ""
for key in statistics:
data = statistics[key]
durations = data["durations"]
data["min"] = min(durations)
data["max"] = max(durations)
data["average"] = sum(durations) / len(durations)
s = "{}\n{} {}\n".format(key, data["average"], data["docstring"])
res += s
res = json.dumps(statistics)
print(res)