A hierarchical performance timer for structured profiling
Project description
TreeTimer
A hierarchical performance timer for measuring nested execution scopes in Python.
Supports context-based timing, named sub-scopes, repeated series tracking, and structured reporting.
Features
- ⏱ Simple
with-based timing API - 🌲 Tree-style nested timing scopes
- 🔁 Support for repeated timing series (e.g. epochs, batches)
- 📦
to_dict()output for visualization or structured logging
Installation
pip install tree-timer
Usage
1. Simple use
from tree_timer import TreeTimer
import time
with TreeTimer() as timer:
time.sleep(0.1)
print(timer)
root: 0.100123s
2. Nested scopes with add_scope()
with TreeTimer() as timer:
with timer.add_scope("load_data"):
time.sleep(0.05)
with timer.add_scope("process_data"):
time.sleep(0.08)
print(timer)
root: 0.130456s
load_data: 0.050123s
process_data: 0.080333s
3. Loop timing with add_series()
with TreeTimer() as timer:
steps = timer.add_series("steps", 3)
for step in steps:
with step:
time.sleep(0.03)
print(timer)
root: 0.090876s
steps: 0.090876s
[0]: 0.030141s
[1]: 0.030251s
[2]: 0.030484s
4. Combined use with parallel execution
from tree_timer import TreeTimer
from concurrent.futures import ThreadPoolExecutor
import time
def run_task(timer):
with timer:
time.sleep(0.03)
with TreeTimer() as timer:
with timer.add_scope("pipeline") as pipeline:
with pipeline.add_scope("load"):
time.sleep(0.02)
steps = pipeline.add_series("parallel_steps", 4)
with ThreadPoolExecutor() as executor:
executor.map(run_task, steps)
with pipeline.add_scope("finalize"):
time.sleep(0.01)
print(timer)
root: 0.063421s
pipeline: 0.063421s
load: 0.020114s
parallel_steps: 0.120548s
[0]: 0.030102s
[1]: 0.030184s
[2]: 0.030120s
[3]: 0.030142s
finalize: 0.010216s
💡 Tasks in
parallel_stepsrun concurrently using ThreadPoolExecutor, while the surroundingloadandfinalizescopes are timed sequentially.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_timer-0.1.0.tar.gz.
File metadata
- Download URL: tree_timer-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5103ce366414ef2d0ff51d568de321fa9db67f316363ad216c1fa2b1d90aabd
|
|
| MD5 |
754ea17f9dd7b5777985ea68aaa3bfc4
|
|
| BLAKE2b-256 |
2e6cb72a13d8e2ade4379a83d7d3575067199f809edda1f5bb57f40eed8fc973
|
File details
Details for the file tree_timer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tree_timer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe02a7a0e4a5f9bd0324f6be1734a3689eba26cb9c4d64a0285a9f64374a7217
|
|
| MD5 |
38e042f3ea0159109db43639ea5b7278
|
|
| BLAKE2b-256 |
2d56811d0999e21593d0d25d9dcfcac3c976914aebdefde8bbde95488631ac30
|