Skip to main content

magic-timer

pip install magic-timer

Conveniently get a rough idea of how long things take.

This is a light wrapper around the standard library's time.monotonic. It aims to provide a clean API, and nice output strings.

How to use:

from magic_timer import MagicTimer

timer = MagicTimer(history=True)
for i in range(3):
    expensive_computation()
    # Print nicely formatted string:
    print(f"{i} - elapsed time {timer}")

# Get the elapsed times that were printed:
print("timer.str_history =", timer.str_history)
0 - elapsed time 510 milliseconds
1 - elapsed time 1.1 seconds
2 - elapsed time 1.6 seconds
timer.str_history = [0.5046274580000158, 1.005028416000016, 1.510260250000016]

Use via context manager:

from magic_timer import MagicTimer

with MagicTimer() as timer:
    # do stuff
    x = sum(i*i for i in range(100_000))

# Print a nicely formatted string:
print('Stuff took', timer)

# Or get the elapsed time in seconds:
time_elapsed = timer.time_elapsed()
print(time_elapsed)
Stuff took 8.0 milliseconds
0.007906290999997623

Use via MagicTimer object:

from magic_timer import MagicTimer

def do_stuff():
    [i*i for i in range(5_000_000)]

timer = MagicTimer()
do_stuff()
print('Stuff took', timer)
do_stuff()
print("Note the timer's still ticking, unless `.stop()` is called...", timer)
Stuff took 210 milliseconds
Note the timer's still ticking, unless `.stop()` is called... 400 milliseconds

To pause the timer, use the stop method (restart with the .start() method). (Note that the context manager automatically calls .stop()).

from magic_timer import MagicTimer

def do_stuff():
    [i*i for i in range(5_000_000)]

timer = MagicTimer()
do_stuff()
timer.stop()
print('Stuff took', timer)
time_elapsed = timer.time_elapsed()
other_stuff()
timer.start()  # continue timing

Use via ftimer decorator:

from magic_timer import ftimer

@ftimer
def do_stuff():
    [i*i for i in range(20_000_000)]

do_stuff()
`do_stuff` ran in 1.9 seconds.

The use case for this package:

You have something you want to time, but you don't want to time it multiple times with timeit.

You also don't want to use Jupyter's %%timeit because it puts the cell into a different scope.

You can import magic-timer, throw it in, and get a rough idea of the time taken. (It's slightly neater than using time.monotonic directly.)

Metadata

Release files for magic-timer 1.0.0

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

Source distribution (sdist)

Source distribution for magic-timer 1.0.0
File Size Uploaded Provenance
magic-timer-1.0.0.tar.gz 4.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for magic-timer 1.0.0
File Interpreter ABI Platform
magic_timer-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / magic-timer-1.0.0.tar.gz

Download URL magic-timer-1.0.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fc440ee58aab16963b497bd8910ae5c747d2bd592cfa374361dced66b03943c6
BLAKE2b-256 checksum
How to use checksums
08273c657007a4c5aff735811c298db7f6645e05bd6187b1c31d7b534b37541d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/0.0.0 CPython/3.9.2

Release files / magic_timer-1.0.0-py3-none-any.whl

Download URL magic_timer-1.0.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8df28d35ab728e0b6be7f36a00e6e04a575a100e6a276014590a6c6d7ca4f856
BLAKE2b-256 checksum
How to use checksums
09c8ef00e73d3c8597c1d52b7b84e5906e10fed1bafe0d90dce420b4514a5621
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/0.0.0 CPython/3.9.2

Security for magic-timer 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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