File tree Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Expand file tree Collapse file tree 1 file changed +17
-5
lines changed Original file line number Diff line number Diff line change 44
55
66class LineWatcher (object ):
7- def __init__ (self , ip ):
8- self .shell = ip
7+ def __init__ (self ):
98 self .start_time = 0.0
109
1110 def start (self ):
1211 self .start_time = time .time ()
1312
1413 def stop (self ):
15- print ('time: %.6f s' % (time .time () - self .start_time ))
14+ diff = time .time () - self .start_time
15+ assert diff > 0
16+ print ('time: %s' % format_delta (diff ))
17+
18+
19+ def format_delta (num , suffix = 's' , threshold = 1000 ):
20+ base_unit = 1000000000
21+ num *= base_unit # smallest unit is 1 / 1000000000 of the biggest unit
22+ for unit in ['n' , 'u' , 'm' , '' ]:
23+ if num < threshold :
24+ return '%3.2f %s%s' % (num , unit , suffix )
25+ num /= threshold
26+ return '%.2f %s' % (num , suffix )
27+
28+
29+ timer = LineWatcher ()
1630
1731
1832def load_ipython_extension (ip ):
19- timer = LineWatcher (ip )
2033 ip .events .register ('pre_run_cell' , timer .start )
2134 ip .events .register ('post_run_cell' , timer .stop )
2235
2336
2437def unload_ipython_extension (ip ):
25- timer = LineWatcher (ip )
2638 ip .events .unregister ('pre_run_cell' , timer .start )
2739 ip .events .unregister ('post_run_cell' , timer .stop )
You can’t perform that action at this time.
0 commit comments