MCPcopy Index your code
hub / github.com/pytorch/tutorials / timed

Function timed

recipes_source/torch_export_aoti_python.py:209–233  ·  view source on GitHub ↗
(fn)

Source from the content-addressed store, hash-verified

207
208import time
209def timed(fn):
210 # Returns the result of running `fn()` and the time it took for `fn()` to run,
211 # in seconds. We use CUDA events and synchronization for accurate
212 # measurement on CUDA enabled devices.
213 if torch.cuda.is_available():
214 start = torch.cuda.Event(enable_timing=True)
215 end = torch.cuda.Event(enable_timing=True)
216 start.record()
217 else:
218 start = time.time()
219
220 result = fn()
221 if torch.cuda.is_available():
222 end.record()
223 torch.cuda.synchronize()
224 else:
225 end = time.time()
226
227 # Measure time taken to execute the function in miliseconds
228 if torch.cuda.is_available():
229 duration = start.elapsed_time(end)
230 else:
231 duration = (end - start) * 1000
232
233 return result, duration
234
235
236######################################################################

Callers 1

Calls 2

recordMethod · 0.80
fnFunction · 0.70

Tested by

no test coverage detected