Skip to main content

Timench

Timench is a small framework for measure execution time of one function, multiple functions and some code with context

Internal variables

Timench has three internal dicts:

  • Timench.funcs = {case_name: func} - for storage funcs for measurement by case name
  • Timench.times = {case_name: [time1, time2, ]} - for storage measurement time (as list) by case name
  • Timench.reports = {case_name: 'report'} for storage for measurement reports (as string) by case name

Usage

1. Time measurement of single function run

For example let's try to measure run time of time.sleep() function

tmnch = Timench()
case_name = 'sleep_1sec'
tmnch.add_func(case_name, time.sleep)  # Add function with case name to benchmark list

Now we can set args for time.sleep(*args) and run benchmark

repeats = 10  # Count of repeats
sleep_time = 1.0  # args of time.sleep(sleep_time)
tmnch.run(case_name, repeats, sleep_time)  # Run benchmark
print(tmnch.get_report(case_name))  # Print benchmark report

and write report to txt-file

tmnch.write_reports('example_1_report.txt')

2. Time measurement of multiple functions run.

My favorite way of Timench usage is compare execution time of different code with same features. Here is basic setup of example:

import time

from timench import Timench

tmnch = Timench()
repeats = 10
env_args = {  # dict structure: {case_name: [args, kwargs] of function func(*args, **kwargs), }
    'sleep_1s': [[1.0, ], None],
    'sleep_2s': [[2.0, ], None],
    'sleep_3s': [[3.0, ], None]
}

for case_name in env_args:  # # Add functions to benchmark list
    tmnch.add_func(case_name, time.sleep)

Run all benchmarks:

tmnch.multiple_run(repeats, env_args)   # Run multiple benchmarks

Output reports to terminal and txt-file:

for case_name in env_args:
    print(tmnch.get_report(case_name))  # Print to terminal all reports

tmnch.write_reports('example_2_report.txt')  # Write all reports to txt-file

3. Context usage

Sometimes we need to know execution times of code without function creation and benchmark setup.

Let's look to simple example:

import time

from timench import Timench

with Timench():  # Just wrap your code with Timench
    time.sleep(1.0)

Full examples

Example 1. Time measurement of function single run

import time

from timench import Timench

tmnch = Timench()
case_name = 'sleep_1sec'  # Set run case name
repeats = 10  # Count of repeats
sleep_time = 1.0  # args of time.sleep(sleep_time)

tmnch.add_func(case_name, time.sleep)  # Add function to benchmark list

tmnch.run(case_name, repeats, sleep_time)  # Run benchmark
print(tmnch.get_report(case_name))  # Print benchmark report

tmnch.write_reports('example_1_report.txt')  # Write all reports to txt-file

Output example:

Case: sleep_1sec
---
Function: sleep
Total time = 10.0128 sec
Best loop time = 1.00024 sec
Average loop time = 1.00128 sec
Repeats = 10

Example 2. Time measurement of multiple functions run

import time

from timench import Timench

tmnch = Timench()
repeats = 10
env_args = {  # dict structure: {case_name: [args, kwargs] of function func(*args, **kwargs), }
    'sleep_1s': [[1.0, ], None],
    'sleep_2s': [[2.0, ], None],
    'sleep_3s': [[3.0, ], None]
}

for case_name in env_args:  # # Add functions to benchmark list
    tmnch.add_func(case_name, time.sleep)

tmnch.multiple_run(repeats, env_args)  # Run multiple benchmarks

for case_name in env_args:
    print(tmnch.get_report(case_name))  # Print to terminal all reports

tmnch.write_reports('example_2_report.txt')  # Write all reports to txt-file

Output example:

Case: sleep_1s
---
Function: sleep
Total time = 10.1691 sec
Best loop time = 1.00017 sec
Average loop time = 1.01691 sec
Repeats = 10

Case: sleep_2s
---
Function: sleep
Total time = 20.0776 sec
Best loop time = 2.00185 sec
Average loop time = 2.00776 sec
Repeats = 10

Case: sleep_3s
---
Function: sleep
Total time = 30.0205 sec
Best loop time = 3.00012 sec
Average loop time = 3.00205 sec
Repeats = 10

Example 3. Time measurement with context execution

import time

from timench import Timench

with Timench():  # Run time measurement for some code
    time.sleep(1.0)
    time.sleep(2.0)
    time.sleep(3.0)

Output example:

Run time = 6.00682 sec

Metadata

Release files for timench 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 timench 0.2.0
File Size Uploaded Provenance Actions
timench-0.2.0.tar.gz 4.3 kB Without provenance Details for timench-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 9.4 kB

Release files / timench-0.2.0.tar.gz

Download URL timench-0.2.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ea2ba6f4f371603cd0486a67e9b8352fb7658fb65676770787d0d6a33f6b3fde
BLAKE2b-256 checksum
How to use checksums
3d4690c313e7b7a5c5fa5b2f54d5646f8be8f6633992a5c256656b86cbb00511
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.9

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

Download URL timench-0.2.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a0ba657fd9dd973ddf6925d08dd37b91425122d288ec1f3f5d4ad573a0206bc
BLAKE2b-256 checksum
How to use checksums
f4bc1bcf6fc04167ed497073b9914606212d8ae1cd586d9e8d2414c2f53f0a16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.6.9

Security for timench 0.2.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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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