-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_speed.py
53 lines (45 loc) · 1.59 KB
/
lib_speed.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
"""
lib_speed module.
This module creates decorators and functions used to calculate
the performance of functions.
"""
from functools import wraps
import time
from pandas import DataFrame, Series
#the dictionary that holds the measures
SPEED_SCORES = {}
def speed_calculate(func):
"""Add the performance score of a function to the dictionary."""
@wraps(func)
def inner(*args, **kwargs):
"""Wrap and execute the decorated function."""
start = time.time()
result = func(*args, **kwargs) #2
if func.__name__ in SPEED_SCORES:
SPEED_SCORES[func.__name__] = (SPEED_SCORES[func.__name__][0] \
+ time.time() - start,\
SPEED_SCORES[func.__name__][1] + 1)
else:
SPEED_SCORES[func.__name__] = (time.time() - start, 1)
return result
return inner
def reset_calculate(func):
"""Reset calculations before this function starts."""
@wraps(func)
def inner(*args, **kwargs):
"""wrap and execute the decorated function."""
SPEED_SCORES.clear()
result = func(*args, **kwargs) #2
return result
return inner
def get_speed_means():
"""
Retrieve the mean scores from calculations.
This should be done before a reset_calculate function finishes.
Returns the mean scores in HTML-ready table representation.
"""
data = {}
for key, item in SPEED_SCORES.iteritems():
data[key] = Series([item[0]/item[1]],\
index=["Average time"])
return DataFrame(data).T.to_html(escape=False)