简体   繁体   中英

Understanding time.clock() and time.time()

Even though they are deprecated and there is a better module than time (ie timeit ), I would like to know the differences between the two functions time.clock() and time.time() .

Starting from the latter ( time.time() ), it basically returns the time elapsed from the 01/01/1970 (if I am not wrong) in seconds with a precision, in general, of 1 second. 'Till here it's easy.

Instead, time.clock() still makes me confused. From the doc:

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Now in Windows it's pretty clear, it does what time.time() does, only with a better precision.

But in Unix/Linux I can't understand. I tried the following code:

import time

def procedure():
  time.sleep(2.5)

# measure process time
t0 = time.clock()
procedure()
print(time.clock() - t0, "process time")

and I got 5.300000000000096e-05 seconds process time which was really wierd to me.

Reading this already-posted question an answer says:

[...] time.clock() measures the amount of CPU time that has been used by the current process.

But I can't get it, the time used by the current process in doing what? When I call time.clock() on Unix I get a float number, what does that mean in that instant? The amout of time consumed by the process from ?? to ??

In a multi-processing system (such as Linux or Windows), several independent processes each run in turn. While one process is running, all of the other processes are waiting * . Occasionally the running process gives up its turn (sometimes cooperatively, sometimes it is forced to stop running). Some other process then gets a turn to run.

This process switch can happen dozens, hundreds or even thousands of times per second. From the human user's perspective, all of the processes appear to be running simultaneously. But in truth, they are all running in turns.

When a process invokes time.sleep(2.5) , it announces that it is giving up the remainder of its current turn, and is not interested in any future turns for at least the next 2.5 seconds. So, for the next 2.5 seconds, it consumes no processor time.

Conversely, if this were the only process in the system:

while True:
    i += 1

it would never give up its turn; it would use 100% of the processor.

So, what has this to do with time.clock() ? In Linux, time.clock() returns the sum of the durations of all of the turns that your process has taken since it was first launched. This is a fair measure of how hard your process's task is. If you only measure wall-clock time (ie time.time() ), then your task's duration would depend upon how many other processes were running and what they were doing.


* This description is valid for single processor multiprocessing systems. For multiprocessor sytems (or multi-core systems), a number of processes actually can run simultaneously. Regardless, time.clock() returns the sum of the durations all of the turns taken.

time.time() returns a value than can be interpreted as the current date and time. time.clock() returns a measure of how much CPU time has been used by the current process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM