关于python中time模块的time.process 和 time.perf_count的关系

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的时间,具体可看我代码中,英文源码的注释。

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有水杯和雨伞的工科男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值