I've been using python's timeit module for some testing, wanted to keep track here of timeit and any other methods I come across. Here's an example usage.


import timeit

start = timeit.default_timer()

#

stop = timeit.default_timer()

print(stop - start)

Note: default_timer uses time.perf_counter() as defined here

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.


Linux

Since I am using linux I can also use a built in bash command of 'time' and run the python program from the command line.

$ time python program.py

This will return output similar to the following.

real 0m4.139s

user 0m0.301s

sys 0m0.053s

A good explanation is found here on stackoverflow.

Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.

Here is a good article about formatting options.


Summary, time is a good benchmarking tool I will be using from the command line, and timeit is one I can insert into my programs and measure any segment I need. timeit Documentation here.


Addendum; Here's an example usage from my program I wrote about in this blog post.

Time values

I used a timeit start at the beginning of the program in the declarations, then I stopped it at the end of the main body after the final print statements. It gives me the first time value of 3.43 seconds, showing that real time measured of the main body of program was 3.43 seconds. Real time includes user prompt and my typing time!

The next time values come from bash's time command, so its real time returned 3.75 seconds. Now this should be comparable to the timeit I ran from within the program, however the bash real will always be greater as it is run outside the program, so it also counts time to open and close the program as well as other boring system things like import statements.

So far I haven't seen a time measure that doesn't include time I spent typing, and that's where the next two values are very useful. They come from bash's time, and they measure process time itself, see details above. So my user mode used my processor for 0.3 seconds (program management) and kernel calculations took 0.026 seconds. The kernel time is the best for comparing actual work performed. I can use this to compare to other programs.

Article: "Measuring program time (timeit module)" by Wolf, in Software

Comments


There are no comments yet.

Add a Comment

You can use the Markdown syntax to format your comment.

Comment Atom Feed

Related content