[Tutor] time taken to execute certain task

Steven D'Aprano steve at pearwood.info
Fri Mar 18 07:16:25 CET 2011

tee chwee liong wrote:
> hi, 
> i would like to know the time taken to execute a certain task in python. i used time.time and time.clock and i see the time taken is different? what is the right method to use? 

Neither, or either. For timing small code snippets, the right tool is 
the timeit module, rather than trying to re-invent the wheel. For large, 
time consuming functions, the difference between the two is irrelevant.

time.time() and time.clock() have different behaviour and different 
performance characteristics. They are platform dependent -- they are 
different from each other, and different depending on your operating system.

On Windows:

time.clock() measures wall clock time and is accurate to better than one 
microsecond; time.time() is only accurate to 1/60th of a second.

On POSIX systems such as Unix, Linux, Apple Macintosh:

time.clock() measures CPU time, but is only accurate to 1/100th of a 
second; time.time() is much more accurate but measures wall clock time.

The timeit module automatically chooses the best timer for your 
operating system, although you can choose another if you prefer.


More information about the Tutor mailing list