-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.py
87 lines (62 loc) · 2.35 KB
/
profiler.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from time import time
import numpy as np
import json
import pandas
import matplotlib.pyplot as plt
class PerformanceProfiler:
def __init__(self):
self.history = {}
def time_function(self, function):
def _wrapper(*args, **kwargs):
t1 = time()
ans = function(*args, **kwargs)
t2 = time()
fn_name = function.__name__
if fn_name not in self.history.keys():
self.history[fn_name] = []
self.history[fn_name].append(t2 - t1)
return ans
return _wrapper
def __how_to_merge_array(self, a, b):
return a + b
def __merge_array_dict(self, a, b, merge_fun = __how_to_merge_array):
shared_keys = set(a.keys()).intersection(b.keys())
a_exclusive_keys = set(a.keys()) - shared_keys
b_exclusive_keys = set(b.keys()) - shared_keys
a_side = {key:a[key] for key in a_exclusive_keys}
b_side = {key:b[key] for key in b_exclusive_keys}
shared_side = {key: merge_fun(a[key], b[key]) for key in shared_keys}
return {**a_side, **b_side, **shared_side}
def apply(self, function):
return {k: function(self.history[k]) for k in self.history.keys()}
def mean(self):
return self.apply(np.mean)
def std(self):
return self.apply(np.std)
def max(self):
return self.apply(np.max)
def min(self):
return self.apply(np.min)
def to_dataframe(self):
log = self.history
durrations = []
function_names = []
for function_name in log.keys():
for value in log[function_name]:
durrations.append(value)
function_names.append(function_name)
return pandas.DataFrame(dict(durrations = durrations,
function = function_name))
def to_json(self, file_name):
with open(file_name, 'w') as fp:
json.dump(self.history, fp)
def from_json(self, file_name):
with open(file_name, 'r') as fp:
self.history = self.merge_array_dict(json.load(fp), self.history)
def plot(self):
for key in self.history.keys():
values = self.history[key]
plt.figure(figsize = (10,2))
plt.hist(values, bins = int(values)//4)
plt.title(key)
plt.show()