Source code for tensorpack.utils.timer

# -*- coding: utf-8 -*-
# File: timer.py


from contextlib import contextmanager
from collections import defaultdict
import six
import atexit
from time import time as timer

from .stats import StatCounter
from . import logger

if six.PY3:
    from time import perf_counter as timer  # noqa


__all__ = ['total_timer', 'timed_operation',
           'print_total_timer', 'IterSpeedCounter']


[docs]@contextmanager def timed_operation(msg, log_start=False): """ Surround a context with a timer. Args: msg(str): the log to print. log_start(bool): whether to print also at the beginning. Example: .. code-block:: python with timed_operation('Good Stuff'): time.sleep(1) Will print: .. code-block:: python Good stuff finished, time:1sec. """ if log_start: logger.info('Start {} ...'.format(msg)) start = timer() yield logger.info('{} finished, time:{:.4f}sec.'.format( msg, timer() - start))
_TOTAL_TIMER_DATA = defaultdict(StatCounter)
[docs]@contextmanager def total_timer(msg): """ A context which add the time spent inside to TotalTimer. """ start = timer() yield t = timer() - start _TOTAL_TIMER_DATA[msg].feed(t)
atexit.register(print_total_timer)
[docs]class IterSpeedCounter(object): """ Test how often some code gets reached. Example: Print the speed of the iteration every 100 times. .. code-block:: python speed = IterSpeedCounter(100) for k in range(1000): # do something speed() """
[docs] def __init__(self, print_every, name=None): """ Args: print_every(int): interval to print. name(str): name to used when print. """ self.cnt = 0 self.print_every = int(print_every) self.name = name if name else 'IterSpeed'
[docs] def reset(self): self.start = timer()
def __call__(self): if self.cnt == 0: self.reset() self.cnt += 1 if self.cnt % self.print_every != 0: return t = timer() - self.start logger.info("{}: {:.2f} sec, {} times, {:.3g} sec/time".format( self.name, t, self.cnt, t / self.cnt))