-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeit.py
executable file
·124 lines (103 loc) · 3.33 KB
/
timeit.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import inspect
import linecache
import time
import functools
import pprint
def reset_timeit():
config = globals()
if 'timeit_GLOBAL_KEY' in config:
config['timeit_GLOBAL_KEY'] = None
def init_timeit():
"""
Initialization function
"""
config = globals()
# reset if any globals() are present
reset_timeit()
GLOBAL_KEY = config['timeit_GLOBAL_KEY'] = 'timeit__GLOBAL__'
config['timeit__FUNC'] = []
config[GLOBAL_KEY] = {}
config[GLOBAL_KEY]['timeit__stats'] = {
'total': 0,
'first_line': None,
'last_line': 0
}
config[GLOBAL_KEY]['timeit__start'] = time.time()
config[GLOBAL_KEY]['timeit__start_int'] = 0
def timeit_stamp(frame=None, show_stats=False):
"""
Usage::
timeit_stamp(inspect.currentframe())
:param frame:
:param show_stats:
"""
config = globals()
GLOBAL_KEY = config['timeit_GLOBAL_KEY']
t = time.time() - config[GLOBAL_KEY]['timeit__start']
stats = config[GLOBAL_KEY]['timeit__stats']
callable_ = linecache.getline(__file__, inspect.getlineno(frame))
lineno = inspect.getlineno(frame)
print '\t *** tracepoint at line: %s %.3f|%s' % (lineno, t,
callable_.strip())
if (stats['total'] < t):
stats['total'] = t
stats['last_line'] = lineno
if stats['first_line'] is None:
stats['first_line'] = lineno
config[GLOBAL_KEY]['timeit__start_int'] = time.time()
if show_stats:
print stats
#==============================================================================
# decorator
#==============================================================================
def timeit(key=None):
def decorator(method):
argspec = inspect.getargspec(method)
config = globals()
GLOBAL_KEY = config['timeit_GLOBAL_KEY']
@functools.wraps(method)
def wrapper(*args, **kwargs):
func_spec = {
'id': id(method),
'method': method,
'argspec': argspec,
}
config['timeit__FUNC_CURRENT'] = func_spec['id']
print '\t *** Measuring function %s' % str(method)
ts = time.time()
result = method(*args, **kwargs)
te = time.time()
func_total = te - ts
func_spec['execution_time'] = func_total
config['timeit__FUNC'].append(func_spec)
print ('\t *** method: "%r" total: %2.2f sec args:(%r, %r) \n') % (
method.__name__, func_total, args, kwargs,
)
print pprint.pformat(config[GLOBAL_KEY]['timeit__stats'])
print pprint.pformat(config['timeit__FUNC'])
return result
return wrapper
return decorator
if __name__ == '__main__':
init_timeit()
print 'aloha !'
@timeit()
def func():
print 'a'
timeit_stamp(inspect.currentframe())
time.sleep(1)
timeit_stamp(inspect.currentframe())
time.sleep(2)
timeit_stamp(inspect.currentframe())
print 'b'
@timeit()
def func2():
print 'a'
timeit_stamp(inspect.currentframe())
time.sleep(3)
timeit_stamp(inspect.currentframe())
time.sleep(4)
timeit_stamp(inspect.currentframe())
print 'b'
func()
func2()