DEV Community

Hrishikesh Terdalkar
Hrishikesh Terdalkar

Posted on

Py-Stopwatch: Measure execution time of your python code segments

GitHub logo hrishikeshrt / py_stopwatch

Python Stopwatch Class

py-stopwatch

https://img.shields.io/pypi/v/py-stopwatch?color=success Documentation Status Python Version Support GitHub Issues GitHub Followers Twitter Followers

Stopwatch class for timing portions of python code.

Features

  • Tick-based stopwatch
  • Support for Pause/Resume
  • Support for multiple named-ticks
  • Utility functions for time between different ticks
  • No third party requirements.

Usage

from stopwatch import Stopwatch
t = Stopwatch()
t.start()
print("Started ..")
time.sleep(0.24)
print(f"t.tick(): {t.tick():.4f} seconds")
time.sleep(0.48)
print(f"t.tick(): {t.tick():.4f} seconds")
time.sleep(0.16)
print(f"t.tick('Named Tick-1'): {t.tick('Named Tick-1'):.4f} seconds")
t.pause()
print("Paused ..")
time.sleep(0.12)
t.resume()
print("Resumed ..")
print(f"t.last(): {t.last():.4f} seconds")
time.sleep(0.12
Enter fullscreen mode Exit fullscreen mode

Features

  • Tick-based stopwatch.
  • Support for Pause / Resume.
  • Support for multiple named-ticks.
  • Utility functions for time between different ticks.
  • No third party requirements.

Usage


from stopwatch import Stopwatch
t = Stopwatch()
t.start()
print("Started ..")
time.sleep(0.24)
print(f"t.tick(): {t.tick():.4f} seconds")
time.sleep(0.48)
print(f"t.tick(): {t.tick():.4f} seconds")
time.sleep(0.16)
print(f"t.tick('Named Tick-1'): {t.tick('Named Tick-1'):.4f} seconds")
t.pause()
print("Paused ..")
time.sleep(0.12)
t.resume()
print("Resumed ..")
print(f"t.last(): {t.last():.4f} seconds")
time.sleep(0.12)
print(f"t.tick(): {t.tick():.4f} seconds")
time.sleep(0.12)
print(f"t.tick('Named Tick-2'): {t.tick('Named Tick-2'):.4f} seconds")
t.stop()
print("Timer stopped.")
print("---")
print(f"Total pause: {t.time_paused:.2f} seconds.")
print(f"Total runtime: {t.time_active:.2f} seconds.")
print(f"Total time: {t.time_total:.2f} seconds.")
tij = t.time_elapsed(start_name='Named Tick-1', end_name='Named Tick-2')
print(f"Time between 'Named Tick-1' and 'Named Tick-2': {tij:.4f}")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)