答案 0 :(得分:3)
time.process_time
不包括子进程花费的时间。这在文档中已明确说明:
返回当前进程的系统和用户CPU时间之和的值(以分数为单位)。
当前进程,而不是当前进程及其所有子进程。
我不知道用于跟踪其子级进程时间的进程的任何接口。我找到的最接近的是POSIX times
函数,该函数提供对等待的子进程的处理时间总和的访问,但这取决于平台并且非常有限。
答案 1 :(得分:1)
为了使理解更加清晰,我编写了一个小脚本来对比处理时间(CPU时间)和时钟时间之间的差异。
此外,它表明子处理时间不包括在CPU时间中。
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