Shortcuts

TimeCounter

class mmengine.utils.dl_utils.TimeCounter(log_interval=1, warmup_interval=1, with_sync=True, tag=None, logger=None)[source]

A tool that counts the average running time of a function or a method. Users can use it as a decorator or context manager to calculate the average running time of code blocks.

Parameters:
  • log_interval (int) – The interval of logging. Defaults to 1.

  • warmup_interval (int) – The interval of warmup. Defaults to 1.

  • with_sync (bool) – Whether to synchronize cuda. Defaults to True.

  • tag (str, optional) – Function tag. Used to distinguish between different functions or methods being called. Defaults to None.

  • logger (MMLogger, optional) – Formatted logger used to record messages. Defaults to None.

Examples

>>> import time
>>> from mmengine.utils.dl_utils import TimeCounter
>>> @TimeCounter()
... def fun1():
...     time.sleep(0.1)
... fun1()
[fun1]-time per run averaged in the past 1 runs: 100.0 ms
>>> @@TimeCounter(log_interval=2, tag='fun')
... def fun2():
...    time.sleep(0.2)
>>> for _ in range(3):
...    fun2()
[fun]-time per run averaged in the past 2 runs: 200.0 ms
>>> with TimeCounter(tag='fun3'):
...      time.sleep(0.3)
[fun3]-time per run averaged in the past 1 runs: 300.0 ms
print_time(elapsed)[source]

print times per count.

Parameters:

elapsed (int | float) –

Return type:

None