MCPcopy Create free account
hub / github.com/THUDM/GLM / Timer

Class Timer

utils.py:116–158  ·  view source on GitHub ↗

Timer.

Source from the content-addressed store, hash-verified

114 """Group of timers."""
115
116 class Timer:
117 """Timer."""
118
119 def __init__(self, name):
120 self.name_ = name
121 self.elapsed_ = 0.0
122 self.started_ = False
123 self.start_time = time.time()
124
125 def start(self):
126 """Start the timer."""
127 assert not self.started_, 'timer has already been started'
128 torch.cuda.synchronize()
129 self.start_time = time.time()
130 self.started_ = True
131
132 def stop(self):
133 """Stop the timer."""
134 assert self.started_, 'timer is not started'
135 torch.cuda.synchronize()
136 self.elapsed_ += (time.time() - self.start_time)
137 self.started_ = False
138
139 def reset(self):
140 """Reset timer."""
141 self.elapsed_ = 0.0
142 self.started_ = False
143
144 def elapsed(self, reset=True):
145 """Calculate the elapsed time."""
146 started_ = self.started_
147 # If the timing in progress, end it first.
148 if self.started_:
149 self.stop()
150 # Get the elapsed time.
151 elapsed_ = self.elapsed_
152 # Reset the elapsed time
153 if reset:
154 self.reset()
155 # If timing was in progress, set it back.
156 if started_:
157 self.start()
158 return elapsed_
159
160 def __init__(self):
161 self.timers = {}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected