Why does time.clock give more elapsed time than time.time?

I timed a section of python code on Ubuntu using time.clock and time.time :

 clock elapsed time: 8.770 s time elapsed time: 1.869 s 

I understand that time.time uses the system time and time .clock uses the processor clock. This makes sense to me when time.time gives a longer elapsed time than time.clock: the processor is just inactive all the time (for example, the time.sleep call time.sleep ).

But why / when does the processor clock give an elapsed time far exceeding the system time?


addition

I did a rough check by calculating the same function with a standard map, with a process pool map and a thread pool map. It’s clear that the process pool is faster and the thread pool is slower. More interesting: clock synchronization is less than time with the processor pool, but more in the thread pool.

Again, I understand why the clock synchronization is less with the processor pool: apparently, the master process does not do much and just waits for the pool processes to complete. But why is there more clock synchronization with the thread pool? Any ideas?

Results:

 map time 1738.8 clock 1739.6 mp pool time 580.1 clock 15.9 thread pool time 3455.3 clock 5378.9 

the code:

 from time import clock, sleep, time from multiprocessing.pool import ThreadPool from multiprocessing import Pool import random def f(i): x = [random.random() for j in range(100000)] return x[i] def t(fn): t0, c0 = time(), clock() for i in range(10): fn(f,range(16)) print ' time ', round(1000*(time()-t0),1) print ' clock', round(1000*(clock()-c0),1) if __name__ == '__main__': print 'map' t(map) pool = Pool(8) print 'mp pool' t(pool.map) pool = ThreadPool(8) print 'thread pool' t(pool.map) 
+5
source share
1 answer

CPU time may exceed wall time if you are running multiple processors. I did not see this in Python, but I definitely saw it when using the clock function with multiple threads from C, and presumably the Python code just calls this C function.

Regarding the "why": you think about it wrong. The important thing is how many cores run your program. If one core works for one second for two seconds of wall time, that makes sense to you, but what if four cores each work for one second in the same amount of time. Then you have 4 seconds of processor time in 2 seconds of time on the wall. The kernel takes into account the processor time, which measures all the cores. If multiple cores are running in one second, then you spent a few seconds on the CPU during that second. It is a measurement of value that matters to the planner, and it is assumed that the metric on which clock built. This may not be the metric you care about, but how it works.

+2
source

Source: https://habr.com/ru/post/1210742/


All Articles