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:
(msg, log_start=False)
| 16 | |
| 17 | @contextmanager |
| 18 | def timed_operation(msg, log_start=False): |
| 19 | """ |
| 20 | Surround a context with a timer. |
| 21 | |
| 22 | Args: |
| 23 | msg(str): the log to print. |
| 24 | log_start(bool): whether to print also at the beginning. |
| 25 | |
| 26 | Example: |
| 27 | .. code-block:: python |
| 28 | |
| 29 | with timed_operation('Good Stuff'): |
| 30 | time.sleep(1) |
| 31 | |
| 32 | Will print: |
| 33 | |
| 34 | .. code-block:: python |
| 35 | |
| 36 | Good stuff finished, time:1sec. |
| 37 | """ |
| 38 | assert len(msg) |
| 39 | if log_start: |
| 40 | logger.info('Start {} ...'.format(msg)) |
| 41 | start = timer() |
| 42 | yield |
| 43 | msg = msg[0].upper() + msg[1:] |
| 44 | logger.info('{} finished, time:{:.4f} sec.'.format( |
| 45 | msg, timer() - start)) |
| 46 | |
| 47 | |
| 48 | _TOTAL_TIMER_DATA = defaultdict(StatCounter) |
no test coverage detected