-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtictoc.py
More file actions
35 lines (25 loc) · 694 Bytes
/
tictoc.py
File metadata and controls
35 lines (25 loc) · 694 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
import time
starts = []
def tic( msg = None ):
if msg is not None:
print(( '+' * (len( starts )+1) ), msg)
starts.append( ( time.clock(), msg ) )
def toc():
end = time.clock()
duration = end - starts[-1][0]
if starts[-1][1] is None:
print(( '=' * len( starts ) ), 'tictoc():', duration)
else:
print(( '-' * len( starts ) ), starts[-1][1], duration)
del starts[-1]
from contextlib import contextmanager
@contextmanager
def tictoc( msg = None ):
tic( msg )
yield
toc()
def tictoc_dec( func ):
def wrapped( *args, **kwargs ):
with tictoc( func.__name__ ):
return func( *args, **kwargs )
return wrapped