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.

Find tutorials and learn how get the most out of the Timer in this section.

Basic Usage and How to Wrap Your Code#

Manual Start and Stop#

After importing the Timer on top of your Python script, simply wrap the Timer function around your code to measure performance of the executed block of code:

Python
1
2
3
4
5
6
7
8
from timer import Timer

timer = Timer()
timer.start()

# Insert your code here

timer.stop()

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

% Elapsed time: 12.34 seconds

Automatic Start and Stop#

Alternatively, use the with statement for context management. This will automatically start and stop the clock – and so no need to declare timer.start() and timer.stop(). Same result as before, but less code:

Python
1
2
3
4
from timer import Timer

with Timer():
    # Insert your code here

How it appears in the terminal:

% Elapsed time: 12.34 seconds

Decimals#

Set decimals to customise the precision of the terminal output:

Python
1
2
3
4
from timer import Timer

with Timer(decimals=5):
    # Insert your code here

How it appears in the terminal:

% Elapsed time: 0.12345 seconds

Multiple Threads#

There's total flexibility to measure the performance of different blocks of code using multiple threads:

Python
1
2
3
4
5
6
7
8
9
from timer import Timer

with Timer(thread="A")
    # Insert your code here

    with Timer(thread="B", decimals=5):
        # Insert more code here

    # Insert even more code here

How it appears in the terminal:

% Elapsed time: 0.12345 seconds for thread B
% Elapsed time: 6.78 seconds for thread A

Support the Project#

Become a Sponsor

If you find this project helpful, please consider supporting its development. Your donations will help keep it alive and growing. Every contribution, no matter the size, makes a difference.

Donate on GitHub Sponsors

Thank you for your support! 🙌