MCPcopy
hub / github.com/facebookresearch/mmf / Timer

Class Timer

pythia/utils/timer.py:5–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5class Timer:
6 DEFAULT_TIME_FORMAT_DATE_TIME = "%Y/%m/%d %H:%M:%S"
7 DEFAULT_TIME_FORMAT = ["%03dms", "%02ds", "%02dm", "%02dh"]
8
9 def __init__(self):
10 self.start = time.time() * 1000
11
12 def get_current(self):
13 return self.get_time_hhmmss(self.start)
14
15 def reset(self):
16 self.start = time.time() * 1000
17
18 def get_time_since_start(self, format=None):
19 return self.get_time_hhmmss(self.start, format)
20
21 def get_time_hhmmss(self, start=None, end=None, gap=None, format=None):
22 """
23 Calculates time since `start` and formats as a string.
24 """
25 if start is None and gap is None:
26
27 if format is None:
28 format = self.DEFAULT_TIME_FORMAT_DATE_TIME
29
30 return time.strftime(format)
31
32 if end is None:
33 end = time.time() * 1000
34 if gap is None:
35 gap = end - start
36
37 s, ms = divmod(gap, 1000)
38 m, s = divmod(s, 60)
39 h, m = divmod(m, 60)
40
41 if format is None:
42 format = self.DEFAULT_TIME_FORMAT
43
44 items = [ms, s, m, h]
45 assert len(items) == len(format), "Format length should be same as items"
46
47 time_str = ""
48 for idx, item in enumerate(items):
49 if item != 0:
50 time_str = format[idx] % item + " " + time_str
51
52 return time_str.strip()

Callers 7

test_get_currentMethod · 0.90
test_resetMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
trainMethod · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Tested by 4

test_get_currentMethod · 0.72
test_resetMethod · 0.72
__init__Method · 0.72