stopwatch.py 935 B

123456789101112131415161718192021222324252627282930313233343536
  1. """Deprecated Stopwatch implementation"""
  2. # Copyright (c) PyZMQ Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. class Stopwatch:
  5. """Deprecated zmq.Stopwatch implementation
  6. You can use Python's builtin timers (time.monotonic, etc.).
  7. """
  8. def __init__(self):
  9. import warnings
  10. warnings.warn(
  11. "zmq.Stopwatch is deprecated. Use stdlib time.monotonic and friends instead",
  12. DeprecationWarning,
  13. stacklevel=2,
  14. )
  15. self._start = 0
  16. import time
  17. try:
  18. self._monotonic = time.monotonic
  19. except AttributeError:
  20. self._monotonic = time.time
  21. def start(self):
  22. """Start the counter"""
  23. self._start = self._monotonic()
  24. def stop(self):
  25. """Return time since start in microseconds"""
  26. stop = self._monotonic()
  27. return int(1e6 * (stop - self._start))