Package: timench
TimenchTimenchis a small framework for measure execution time of one function, multiple functions and some code with contextInternal variablesTimench has three internal dicts:Timench.funcs = {case_name: func} - for storage funcs for measurement by case nameTimench.times = {case_name: [time1, time2, ]} - for storage measurement time (as list) by case nameTimench.reports = {case_name: 'report'} for storage for measurement reports (as string) by case nameUsage1. Time measurement of single function runFor example let's try to measure run time of time.sleep() functiontmnch=Timench()case_name='sleep_1sec'tmnch.add_func(case_name,time.sleep)# Add function with case name to benchmark listNow we can set args for time.sleep(*args) and run benchmarkrepeats=10# Count of repeatssleep_time=1.0# args of time.sleep(sleep_time)tmnch.run(case_name,repeats,sleep_time)# Run benchmarkprint(tmnch.get_report(case_name))# Print benchmark reportand write report to txt-filetmnch.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:importtimefromtimenchimportTimenchtmnch=Timench()repeats=10env_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]}forcase_nameinenv_args:# # Add functions to benchmark listtmnch.add_func(case_name,time.sleep)Run all benchmarks:tmnch.multiple_run(repeats,env_args)# Run multiple benchmarksOutput reports to terminal and txt-file:forcase_nameinenv_args:print(tmnch.get_report(case_name))# Print to terminal all reportstmnch.write_reports('example_2_report.txt')# Write all reports to txt-file3. Context usageSometimes we need to know execution times of code without function creation and benchmark setup.Let's look to simple example:importtimefromtimenchimportTimenchwithTimench():# Just wrap your code with Timenchtime.sleep(1.0)Full examplesExample 1. Time measurement of function single runimporttimefromtimenchimportTimenchtmnch=Timench()case_name='sleep_1sec'# Set run case namerepeats=10# Count of repeatssleep_time=1.0# args of time.sleep(sleep_time)tmnch.add_func(case_name,time.sleep)# Add function to benchmark listtmnch.run(case_name,repeats,sleep_time)# Run benchmarkprint(tmnch.get_report(case_name))# Print benchmark reporttmnch.write_reports('example_1_report.txt')# Write all reports to txt-fileOutput example:Case: sleep_1sec
---
Function: sleep
Total time = 10.0128 sec
Best loop time = 1.00024 sec
Average loop time = 1.00128 sec
Repeats = 10Example 2. Time measurement of multiple functions runimporttimefromtimenchimportTimenchtmnch=Timench()repeats=10env_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]}forcase_nameinenv_args:# # Add functions to benchmark listtmnch.add_func(case_name,time.sleep)tmnch.multiple_run(repeats,env_args)# Run multiple benchmarksforcase_nameinenv_args:print(tmnch.get_report(case_name))# Print to terminal all reportstmnch.write_reports('example_2_report.txt')# Write all reports to txt-fileOutput 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 = 10Example 3. Time measurement with context executionimporttimefromtimenchimportTimenchwithTimench():# Run time measurement for some codetime.sleep(1.0)time.sleep(2.0)time.sleep(3.0)Output example:Run time = 6.00682 sec