Measure the duration of python code by creating an timer objects with only a name.
Two simple time measurement classes: Timer is single entrant and measure time since last occurrence. MultiTimer remembers the name for a measure point and can be used in loops.
Display results by calling a class function
Not thread save, but if you want to do performance tests for such a program, you should not be looking at a timer with 'simple' in it's name.
import time
from simplepytimer import MultiTimer
MultiTimer("start")
time.sleep(0.1)
MultiTimer("end")
MultiTimer.print_timings()- Construct the object with only a string and create a measure point
- Optional jump in level for displaying results
- Optionally exclude most of the timer work in the timing results
- Optionally export to file (csv, standard), with # comments
- Reset internal state to Null
- Only standard imports
- Self test when running the python file.
Construct two timer object with a name to start a measurement. Construct more to get durations between the timer. call print_timings to get the results on the screen
See run_example() for a detailed use-cases with line by line comments
Constructor initializes a measure point. You need a minimum
of two to measure a stretch of time.
name: string identifier, printed in the results
level: how much to jump in when printing
[exclude_timer_work]: bool option available for the MultiTimer. Do not
measure some calculations internal in the timer. The timing might be
a little better but the total duration of the programm will no be the
same anymore as measured in the timer.
Clears the timer as if not being seen before.
Display the results currently stored in the timer (can be called multuple times)
header: Print a header with info of metrics before printing results
seperator: seperator between metrics
prefix: What character to print before name to jump in.
Print
fp: 'file like' object to send output data to
header: Print explaining header
#Total run time time (sec) : 1.40700006485
#% : name : count : total time spend (sec)
0 : Start : 1 : 0.0
7 : Second timer : 1 : 0.101000070572
7 : Third timer, with a jump in : 1 : 0.0999999046326
14 : Fourth timer : 2 : 0.200999975204
71 : Fifth timer seen 10 times! : 10 : 1.00500011444
Second timing results:
#Total run time time (sec) : 1.21000003815
#% : name : count : total time spend (sec)
0 : Start : 1 : 0.0
0 : loop : 7 : 0.000999927520752
0 : Timer 1 : 1 : 0.0
0 : Timer 10 : 10 : 0.0
0 : Timer 100 : 100 : 0.0
0 : Timer 1000 : 1000 : 0.00500011444092
3 : Timer 10000.0 : 10000 : 0.0319998264313
10 : Timer 100000.0 : 100000 : 0.117000102997
87 : Timer 1000000.0 : 1000000 : 1.05500006676
Timing of the timer:
#Total run time time (sec) : 1.21600008011
#% : name : total time spend (sec)
0 : start : 0.0
0 : Timer 1 : 0.0
0 : Timer 10 : 0.0
0 : Timer 100 : 0.0
0 : Timer 1000 : 0.00500011444092
3 : Timer 10000.0 : 0.0319998264313
10 : Timer 100000.0 : 0.118000030518
87 : Timer 1000000.0 : 1.06100010872
Press any key to continue . . .
- Timer is not as feature complete as MultiTimer
- Better function doc strings
- Refactor out common print capability
- Decorator?
- Python 3?
Keywords: python timer timing timeit performance runtime duration