Skip to main content

Named timers for measuring multiple operations within a function or script

Project description

philiprehberger-task-timer

Tests PyPI version Last updated

Named timers for measuring multiple operations within a function or script.

Installation

pip install philiprehberger-task-timer

Usage

from philiprehberger_task_timer import TaskTimer

t = TaskTimer()

with t.task("load data"):
    data = load_from_disk()

with t.task("process"):
    result = process(data)

with t.task("save"):
    save(result)

t.summary()

Output:

load data    0.52s  (26%)
process      1.23s  (62%)
save         0.24s  (12%)

Nested tasks

Tasks can be nested. Nested tasks appear indented in the summary output.

t = TaskTimer()

with t.task("pipeline"):
    with t.task("step 1"):
        do_step_1()
    with t.task("step 2"):
        do_step_2()

t.summary()

Output:

pipeline      2.05s  (100%)
  step 1      0.80s  (39%)
  step 2      1.25s  (61%)

Laps

Use lap() to record incremental time without context managers.

t = TaskTimer()
load()
t.lap("load")
process()
t.lap("process")
t.summary()

as_dict

Retrieve raw timing data as a dictionary.

t = TaskTimer()
with t.task("work"):
    do_work()

print(t.as_dict())  # {"work": 1.234}

Async support

The task() context manager works with async with as well.

t = TaskTimer()
async with t.task("fetch"):
    await fetch_data()
t.summary()

API

Method / Property Description
task(name) Context manager that times the enclosed block (sync and async)
lap(name) Record time since the last lap or timer creation
summary(*, file=sys.stdout) Print formatted summary with name, duration, and percentage
as_dict() Return dict[str, float] of task name to seconds
total Property returning the total time across all tasks
reset() Clear all recorded tasks

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

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

philiprehberger_task_timer-0.1.7.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_task_timer-0.1.7-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_task_timer-0.1.7.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_task_timer-0.1.7.tar.gz
Algorithm Hash digest
SHA256 fab41269f7f9aa78c526a6c67cae582ddb9e49f3b763e35388e310a7027bbec8
MD5 0e1284e6da16dd8ebf67ff8e3a630acc
BLAKE2b-256 86b7d54d1ce449b30fc7a5e8ae5bfb58086411b6841340409afbc18b14251399

See more details on using hashes here.

File details

Details for the file philiprehberger_task_timer-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_task_timer-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 9d93f0fa8289575746b41e02975204905074a791c859365117468c7e9653d721
MD5 1c30cdd47f8b033a787bb8eb6c89e775
BLAKE2b-256 eed4e50d545e44643a1b2c71817af4f6b05a9268b998248a71e43e593a3e1469

See more details on using hashes here.

Supported by

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