关于python中time.perf_counter() 与 time.process_time()分析与疑问

@[TOC](time.perf_counter() 与 time.process_time()分析与疑问)

关于python中的time类

最近的项目中由于需要测试程序运行的时间,用到了time类中的time.clock(),运行之后被提示

DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

才发现clock已经被time.per_countertime.process_time替代,遂想详细了解一下time类中的常用定时器,包括

  1. time.clock()
  2. time.monotonic()
  3. time.thread_time()
    但是实测python3.7.4中仍然无法使用,虽然官方文档中有写
    Availability: Windows, Linux, Unix systems supporting .
    但是mac下shell中确实不能用,很迷
  4. time.perf_counter()
  5. time.process_time()
  6. time.time()

根据time内置的方法time.get_clock_info('nameOfClock')可以获得上述几种定时器的详细描述,信息如下表所示

定时器类型djustableimplementationmonotonicresolution
clockFalseclock()True1e-06
monotonicFalsemach_absolute_time()True1e-09
perf_counterFalsemach_absolute_time()True1e-09
process_timeFalsegetrusage(RUSAGE_SELF)True1e-06
timeTruegettimeofday()False1e-06

adjustable: True表示该时间可以由系统管理员进行更改,比如你手动设置时间
implement:表示该方法参数经由系统底层C函数获取
monotonic:True表示时间不可以回退,反之
resolution:表示时间精度,类似于频率

clock已经过时就不提了,time因为可以由系统改变,不适合于测试时间差也排除,thread_time根据文档的描述和process_time类似,分别计算线程和进程的时间。咱主要分析一下perf_counterprocess_time。从上述表格中resolution可以看到在resolution的区别上比较明显,perf_counter精度要比process_time高(或者说采集频率高),如下测试

process_time = 0;
perf_counter= 0;
num1,num2 = 0,0;
for i in range(0,1000000):
    t = time.process_time();
    if t!=process_time:
        num1+=1
    process_time = t
print("在迭代1000000次的过程中process采样了"+str(num1)+"次")
for i in range(0,1000000):
    t = time.perf_counter();
    if t!=perf_counter:
        num2+=1
    perf_counter = t
print("在迭代1000000次的过程中perf_counter采样了"+str(num2)+"次")

输出:

在迭代1000000次的过程中process采样了570565次`
在迭代1000000次的过程中perf_counter采样了1000000次

即在不考虑程序中包含例如time.sleep()人为延迟的情况下(process_time可以忽略这类延迟只计算CPU在线程上的工作时间),使用perf_counter可以获得更加准确的时间差。

问题

按理来说对于正常的程序运行时间perf和process测得时间应该差不多(没有sleep的情况下),但是实际测下来process_time所的到的时间大约为真实时间的2倍= =,perf_counter测得是实际的程序运行时间(和以前的clock做对比)如下

a1=time.perf_counter()
a2=time.process_time()
a3 = time.clock()
c=1
for i in range(1,200000):
    c*=i
b1=time.perf_counter()
b2=time.process_time()
b3=time.clock()
print(b1-a1,'s')
print(b2-a2,'s')
print(b3-a3,'s')

输出:

11.361218348000001 s
22.448002000000002 s
11.324845 s

为什么process_time会是正常值的两倍呢?寻思难道和线程有关?计算了两个线程运行时间的总和?也没有其他线程呀。总之目前还是用perf_counter比较稳妥一点。希望能和大家一起讨论

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值