Для большей ясности в понимании я написал небольшой скрипт, который сравнивает разницу между временем процесса (время процессора) и временем часов.
Кроме того, это показывает, что время дочерних процессов не включено в процессорное время.
import datetime
import time
from multiprocessing import Pool
from numpy import mean
def f(x):
i = 0
for j in range(x ** 8):
i += j
return i
def process_time():
clock_t0 = time.time()
t0 = time.process_time()
result = f(9)
print('Result:', result, end='\t')
t1 = time.process_time()
clock_t1 = time.time()
print('CPU time: ', t1 - t0, end='\t')
print('Clock time: ', clock_t1 - clock_t0)
return t1 - t0
def multiprocessing_process_time():
clock_t0 = time.time()
t0 = time.process_time()
with Pool(10) as pool:
result = pool.map(f, [9])
print('Result:', result[0], end='\t')
t1 = time.process_time()
clock_t1 = time.time()
print('CPU time: ', t1 - t0, end='\t')
print('Clock time: ', clock_t1 - clock_t0)
return t1 - t0
if __name__ == '__main__':
print('Processing in Parent Process\n')
print('Mean CPU processing time:', mean([process_time() for _ in range(5)]))
print('\nProcessing in Child Process')
print('Mean CPU processing time:', mean([multiprocessing_process_time() for _ in range(5)]))
Вывод вышеуказанной программы:
Processing in Parent Process
Result: 926510072902560 CPU time: 2.620428 Clock time: 2.6484527587890625
Result: 926510072902560 CPU time: 2.6250959999999997 Clock time: 2.654899835586548
Result: 926510072902560 CPU time: 2.587252000000001 Clock time: 2.6077020168304443
Result: 926510072902560 CPU time: 2.6254989999999996 Clock time: 2.667827844619751
Result: 926510072902560 CPU time: 2.5997120000000002 Clock time: 2.6256277561187744
Mean CPU processing time: 2.6115974
Processing in Child Process
Result: 926510072902560 CPU time: 0.025433999999998846 Clock time: 2.701629877090454
Result: 926510072902560 CPU time: 0.0210480000000004 Clock time: 2.8027760982513428
Result: 926510072902560 CPU time: 0.02214200000000055 Clock time: 2.8002538681030273
Result: 926510072902560 CPU time: 0.02209799999999973 Clock time: 2.7950479984283447
Result: 926510072902560 CPU time: 0.03242999999999974 Clock time: 2.718341112136841
Mean CPU processing time: 0.0246304