Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

WITHTIMER

Description

WithTimer is a Python module for benchmarking your code. It has the following features:

  • Simple use
    • Just create a timer object, and it times execution until it goes out of scope
  • Arbitrarily nested timers
    • Create as many nested timers as you want
    • Each timer pushes itself to a stack, and pops itself off when destroyed

Example usage

with Timer(name="Something slow"):
  do_something_slow()

Timing is turned off by default, and can be optionally turned on. You can instrument your code with timers, and leave them in when you're not using them:

if args.enable_timing:
  Timer.enable_timing(args.enable_timing)
with Timer(name="Timers are a sometimes treat"):
  do_something()

You can nest timers:

with Timer(name="Outer timer"):
  for i in range(0, 100000):
    with Timer(name="Hope this isn't O(n^2)"):
      do_something_critical()

Here's a complete example:

from time import sleep
from withtimer.withtimer import Timer


def func1(count):
    for i in range(0, 3):
        with Timer(name="Calling func2"):
            func2(2)
    print("func1 sleeping {}".format(count))
    sleep(count)


def func2(count):
    print("func2 sleeping {}".format(count))
    sleep(count)


if __name__ == "__main__":
    Timer.enable_timing(True)
    with Timer(name="main"):
        for i in range(0, 3):
            if i % 2 > 0:
                func1(i+1)

Output:

$ python3  ./example.py
[main] start
    [Calling func2] start
func2 sleeping 2
    [Calling func2] end	Elapsed time: 2.003 seconds

    [Calling func2] start
func2 sleeping 2
    [Calling func2] end	Elapsed time: 2.003 seconds

    [Calling func2] start
func2 sleeping 2
    [Calling func2] end	Elapsed time: 2.003 seconds

func1 sleeping 2
[main] end	Elapsed time: 8.015 seconds
$

Metadata

Release files for withtimer 0.1.0.dev1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for withtimer 0.1.0.dev1
File Size Uploaded
withtimer-0.1.0.dev1.tar.gz 3.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for withtimer 0.1.0.dev1
File Interpreter ABI Platform
withtimer-0.1.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / withtimer-0.1.0.dev1.tar.gz

Download URL withtimer-0.1.0.dev1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
10440efb6121f6be50da0a3354f938c694d7d50058d596a5a9fe59a5d97dcadd
BLAKE2b-256 checksum
How to use checksums
0e733968a43af63c06fddf02aec7527b1f1881acf793e448e2a60bdf90f0316d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

Release files / withtimer-0.1.0.dev1-py3-none-any.whl

Download URL withtimer-0.1.0.dev1-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
129aec4434bb6f2adeae5bbd6dcae383a58ee1b28e6039440a8f24262f29c966
BLAKE2b-256 checksum
How to use checksums
6e3aa6127772d321c5faa8e180b2c2ced5ac8537a45bb5b8745706ae99c96b02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.1.0.dev1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page