-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimer.py
31 lines (25 loc) · 900 Bytes
/
Timer.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
from __future__ import print_function
import datetime
class Timer:
def __init__(self, msg='elapsed'):
self.msg = msg
def __enter__(self):
self.start = datetime.datetime.now()
return self
def __exit__(self, type, value, traceback):
s = [ self.msg ]
s += [ self.time() ]
if value:
s += [ '%s'%type ]
s += [ '%s'%value ]
print( ' '.join(s) )
def seconds(self, N=1):
return (datetime.datetime.now() - self.start).total_seconds()*N
def time(self, N=1):
t = self.seconds(N)
if t > 60*60:
s = '%sh%sm%ss%sms' %( int(t)/60/60, int(t/60)%60, int(t)%60, int(t*1e3)%1000 )
else:
if t > 60: s = '%sm%ss%sms' %( int(t)/60, int(t)%60, int(t*1e3)%1000 )
else: s = '%ss%sms' % (int(t), int(t*1e3)%1000 )
return s