Skip to main content

philiprehberger-timerfunc

Tests PyPI version Last updated

Measure execution time of any function.

Installation

pip install philiprehberger-timerfunc

Usage

Context Manager

from philiprehberger_timerfunc import timer

with timer() as t:
    do_work()
print(f"Took {t.elapsed_ms:.1f}ms")

Decorator

from philiprehberger_timerfunc import timed

@timed
def process_data():
    ...  # logs: "process_data took 42.3ms"

@timed(threshold_ms=100)
def api_call():
    ...  # only logs if slower than 100ms

Benchmark

from philiprehberger_timerfunc import benchmark

result = benchmark(my_function, args=(data,), iterations=1000)
print(f"Mean: {result.mean_ms:.2f}ms, P95: {result.p95_ms:.2f}ms")
print(f"Fastest: {result.fastest:.2f}ms, Slowest: {result.slowest:.2f}ms")
print(result)  # full stats summary

Comparing benchmarks

from philiprehberger_timerfunc import benchmark, compare_benchmarks

old = benchmark(old_impl, iterations=1000)
new = benchmark(new_impl, iterations=1000)

ratio = compare_benchmarks(new, old)
# ratio > 1 means `new` is faster than `old` (old took proportionally longer)
print(f"new is {ratio:.2f}x faster than old")

API

Function / Class Description
timer() Context manager returning TimerResult
@timed / @timed(threshold_ms=0) Decorator logging execution time
benchmark(fn, args, kwargs, iterations, warmup) Returns BenchmarkResult
BenchmarkResult.fastest Duration of the fastest run (ms); alias of min_ms
BenchmarkResult.slowest Duration of the slowest run (ms); alias of max_ms
compare_benchmarks(a, b) Returns b.mean / a.mean; values > 1 mean a is faster

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-timerfunc 0.2.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 philiprehberger-timerfunc 0.2.0
File Size Uploaded
philiprehberger_timerfunc-0.2.0.tar.gz 173.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-timerfunc 0.2.0
File Interpreter ABI Platform
philiprehberger_timerfunc-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 178.3 kB

Release files / philiprehberger_timerfunc-0.2.0.tar.gz

Download URL philiprehberger_timerfunc-0.2.0.tar.gz
Size 173.1 kB
Tags Source
SHA-256 checksum
How to use checksums
32f79823400e91f66c4fb0dcf827b085c8eb5955151b9c0aaf365d16570339b3
BLAKE2b-256 checksum
How to use checksums
ee478656df09c5df36597368036df0c8ddf60ffa7d4d12f87121b503248ad71f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_timerfunc-0.2.0-py3-none-any.whl

Download URL philiprehberger_timerfunc-0.2.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ea75a1ab61d2ffaec7be76d2282350b8711df3cb35cdb364dc374b202e18539
BLAKE2b-256 checksum
How to use checksums
b71a528262e00865e4f76f4dc38e2bc15f3660574cacd74447ddad0c20d4f668
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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