Skip to main content

Timer for Python

Project description

Latest version Python 3.10 | 3.11 | 3.12 | 3.13+ MIT license Codecov CodeQL Test Downloads

⏳ Timer for Python ⌛️

Lightweight Python package that makes it easy to measure how much time it takes to run Python programs and gauge performance of multiple, smaller bits of code.

Ready to try? Learn how to install and find tutorials in the user guide.

Getting Started

Basics

Simply add the Timer to your imports, and then wrap the Timer function around your code to measure the performance of the executed block of code:

from timer import Timer

timer = Timer()
timer.start()

# Insert your code here

timer.stop() # Output example: 12.34 seconds

Context Manager

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

with Timer():
    # Insert your code here

    # Output example: 12.34 seconds

Decorator

Or use the benchmark_timer as a function decorator:

from timer import benchmark_timer

@benchmark_timer
def test_function():
    # Insert your code here

test_function()

# Output example: 12.34 seconds for thread TEST_FUNCTION

Core Features

Decimals

Instead of the default value of 2 for decimals``, you can set the output precision up to 9in thedecimals` argument:

timer = Timer()
timer.start(decimals=5)

# Insert your code here

timer.stop() # Output example: 0.12345 seconds

Multiple Threads

Imagine that you want to troubleshoot which parts of your code are performing better or worse. Or do you want to split-test the performance of different methods? Timer for Python is a quick, easy way to get the job done.

To measure the performance of multiple blocks of code, use the thread argument to name different threads:

timer = Timer()
timer.start(thread="A")

# Insert your code here

    timer.start(thread="B", decimals=5)

    # Insert more code here

    timer.stop(thread="B") # Output example: 0.12345 seconds for thread B

# Insert even more code here

timer.stop(thread="A") # Output example: 6.78 seconds for thread A

Or use the with statement to get the same result with less code:

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

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

        # Output example: 0.12345 seconds for thread B

    # Insert even more code here

    # Output example: 6.78 seconds for thread A

Thank You for Supporting

Donate

This module is free to use. And if you like it, feel free to buy me a coffee.

Contribute

If you have suggestions or changes to the module, feel free to add to the code and create a pull request.

Report Bugs

Report bugs and issues here.

MIT License

Copyright (c) 2020 – present, Jakob Bagterp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timer-for-python-0.8.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

timer_for_python-0.8.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file timer-for-python-0.8.0.tar.gz.

File metadata

  • Download URL: timer-for-python-0.8.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for timer-for-python-0.8.0.tar.gz
Algorithm Hash digest
SHA256 35fe64eb1f1025519c05e881b0378de485e78ffafb64bd21deb934634ee0de74
MD5 51af87db207c9a7cf2d6b42c8b7bc874
BLAKE2b-256 f934ddc302d7bf0823655d3ca98251af6e8a3026516e43e3a94fa58279a1bc08

See more details on using hashes here.

Provenance

The following attestation bundles were made for timer-for-python-0.8.0.tar.gz:

Publisher: release.yml on jakob-bagterp/timer-for-python

Attestations:

File details

Details for the file timer_for_python-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for timer_for_python-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 59e74990907b3d626a45ff91da254368db2596f3598ecd084902793b2f04beff
MD5 7e2c66bcf71a873da56825be56acddf8
BLAKE2b-256 9b359762945fb735c2bae542e5a55d8369f274ce7558268f30dffdbf87e4b8cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for timer_for_python-0.8.0-py3-none-any.whl:

Publisher: release.yml on jakob-bagterp/timer-for-python

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page