File: utils.py

package info (click to toggle)
pytorch-text 0.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,560 kB
  • sloc: python: 14,197; cpp: 2,404; sh: 214; makefile: 20
file content (30 lines) | stat: -rw-r--r-- 786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import time


class Timer:
    """Basic utility class to calculate execution time. It can also be used a context manager."""

    def __init__(self, text=""):
        self._text = text
        self._start = None

    def start(self):
        if self._start is not None:
            raise Exception("Timer is already running. Call .stop() to stop it")

        self._start = time.perf_counter()

    def stop(self):
        if self._start is None:
            raise Exception("Timer is not running. Call .start() to start the timer.")

        elapsed = time.perf_counter() - self._start

        print("{} ... Total running time: {}".format(self._text, elapsed))

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, *exc_info):
        self.stop()