Skip to content

User Guide 👨‍🔧#

Timer for Python is a lightweight package and intended to be easy to use. A simple tool for measuring performance of Python programs or blocks of code.

Basics#

Define Imports#

Firstly, import the Timer on top of your Python script:

Python
from timer import Timer

Wrap Your Code with Start and Stop#

Wrap the Timer function around your code to measure performance of the executed block of code:

Python
3
4
5
6
7
8
timer = Timer()
timer.start()

# Insert your code here

timer.stop()

After timer.stop(), the elapsed time will be printed in your terminal. Example:

Elapsed time: 12.34 seconds

Core Features#

When the basics aren't sufficient, Timer for Python also offers more advanced features to measure performance of specific code blocks:

  • threads,
  • function decorators,
  • and more.

A few excerpts:

Multiple Threads#

Set multiple threads to measure performance of different blocks of code:

timer.start(thread="A") >-------|
# Insert your code here         |
                                |
timer.start(thread="B") >---|   |
for i in range(100):        |   |
    # Insert more code here |   |
timer.stop(thread="B") <----|   |
                                |
timer.stop(thread="A") <--------|

Decimals#

Set decimals to customise the precision of the terminal output:

timer.start(decimals=5)

Context Manager#

Avoid using start() and stop() by applying the built-in context manager with statement. This can be used in combination with decimals and multiple threads:

with Timer(thread="A"): >----------------------|
    # Insert your code here                    |
                                               |
    with Timer(thread="B", decimals=5): >--|   |
        # Insert more code here            |   |
        |<---------------------------------|   |
    |<-----------------------------------------|

Function Decorator#

How to apply the function decorator to measure performance of functions:

@benchmark_timer
def some_function():
    # Function code