import time
'''
根据python 官方手册对process_time 以及perf_counter的定义:
time.process_time() → float
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process.
It does not include time elapsed during sleep. It is process-wide by definition.
The reference point of the returned value is undefined,
so that only the difference between the results of consecutive calls is valid.
time.perf_counter() → float
Return the value (in fractional seconds) of a performance counter,
i.e. a clock with the highest available resolution to measure a short duration.
It does include time elapsed during sleep and is system-wide.
The reference point of the returned value is undefined,
so that only the difference between the results of consecutive calls is valid.
'''
print('测试time.slepp 对 process_time,perfcount的关系的影响:')
start_time_process = time.process_time()
start_time_perf = time.perf_counter()
time.sleep(10)
end_time_process = time.process_time()
print('process_time:',end_time_process-start_time_process)
end_time_perf = time.perf_counter()
print('perf_time:',end_time_perf-start_time_perf)
start_time = time.process_time()
time.sleep(1)
end_time = time.process_time()
print('延时1s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.1)
end_time = time.perf_counter()
print('延时0.1s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.01)
end_time = time.perf_counter()
print('延时0.01s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.001)
end_time = time.perf_counter()
print('延时0.001s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.0001)
end_time = time.perf_counter()
print('延时0.0001s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.00001)
end_time = time.perf_counter()
print('延时0.00001s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.000001)
end_time = time.perf_counter()
print('延时0.000001s',end_time-start_time)
start_time = time.perf_counter()
time.sleep(0.0000001)
end_time = time.perf_counter()
print('延时0.0000001s',end_time-start_time)
print('通过测试的结论,起最高的精度不超过10%的情况下:1000us')
print('通过测试的结论,起最高的精度不超过50%的情况下:100us')
上面所示的代码,就是我用来测试:
主要测试两个问题:
1、time.sleep 的精度问题
2、time.perf_count 与 time.process_time 的区别
得到的结论如下:
1、time.sleep的精度也就只能达到1ms的量级,如果需要更好的定时的操作,需要自己写额外的 操作
2、time.perf_count 其中计算的是cpu 的时间,其中包括了time.sleep的时间,time.process_time的时间,就不包括time.sleep的时间,具体可看我代码中,英文源码的注释。