Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.py 958B

12345678910111213141516171819202122232425262728293031323334353637
  1. import time
  2. class Timer(object):
  3. """A simple timer."""
  4. def __init__(self):
  5. self.total_time = 0.
  6. self.calls = 0
  7. self.start_time = 0.
  8. self.diff = 0.
  9. self.average_time = 0.
  10. self.duration = 0.
  11. def tic(self):
  12. # using time.time instead of time.clock because time time.clock
  13. # does not normalize for multithreading
  14. self.start_time = time.time()
  15. def toc(self, average=True):
  16. self.diff = time.time() - self.start_time
  17. self.total_time += self.diff
  18. self.calls += 1
  19. self.average_time = self.total_time / self.calls
  20. if average:
  21. self.duration = self.average_time
  22. else:
  23. self.duration = self.diff
  24. return self.duration
  25. def clear(self):
  26. self.total_time = 0.
  27. self.calls = 0
  28. self.start_time = 0.
  29. self.diff = 0.
  30. self.average_time = 0.
  31. self.duration = 0.