time.clock() or time.time()

Magnus Lycka lycka at carmen.se
Tue Aug 2 19:00:21 CEST 2005


Shane Hathaway wrote:
> time.time() measures real time, while time.clock() measures the time the 
> CPU dedicates to your program. 

I suppose that varies with the platform... "help(time.clock)" says:

Help on built-in function clock:

clock(...)
     clock() -> floating point number

     Return the CPU time or real time since the start of the process or 
since
     the first call to clock().  This has as much precision as the 
system records.

Another thing to notice is that depending on OS, either time.time() or
time.clock() might have much higher precision than the other.

For Linux on Intel at least, you'll probably want to always use
time.time(). On Windows, you're likely to prefer time.clock(), to
measure relative times, since time.time() will have too low resolution
for measuring short thingies. Be aware that time.clock() will restart
at 0 now and then though. I.e. t1=time.clock();f();t2=time.clock() will
have t1>t2 now and then. Using time.time(), that won't happen until
after I retire... :)

For Linux, see "man 2 time" and "man 3 clock".




More information about the Python-list mailing list