关于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
    评论
time.perf_counter()和time.time()是两个用于测量时间的方法,它们之间存在一些区别。 time.perf_counter()返回性能计数器的值,以小数秒为单位,具有最高可用分辨率的时钟,用于测量短持续时间。它包括了睡眠期间经过的时间,并且是系统范围的。通常在测试代码时间上使用perf_counter(),因为它具有最高的可用分辨率。需要注意的是,perf_counter()的参考点未定义,因此在测试代码时间时需要调用两次,并且计算两次调用之间的差值,以获得准确的时间间隔。 而time.time()返回自纪元以来的秒数作为浮点数,具体日期和闰秒的处理取决于使用的平台。它通常用于时间的格式化输出,可以将返回的数字转换为更常见的时间格式,如年、月、日、小时等。需要注意的是,不是所有系统都提供比1秒更高的精度,并且更改系统时间会影响time()的值。此外,time()的返回值是递增的,但如果在两次调用之间设置了系统时钟,则可能会返回比先前调用更低的值。 综上所述,time.perf_counter()适用于测量短持续时间,具有最高的分辨率,而time.time()用于获取当前时间和时间格式化输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python3.7time模块的time()、perf_counter()和process_time()的区别](https://blog.csdn.net/qq_27283619/article/details/89280974)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值