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)
source share