Python记录程序运行的时间

本文介绍了如何使用Python的time模块和uiautomator2库来测量自动化任务执行时间,特别关注了在Python 3.8及以上版本中替换time.clock的方法。通过实例演示了从启动APP到主界面的计时过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python记录程序运行的时间

内置模块time包含很多与时间相关函数。我们可通过它获得当前的时间和格式化时间输出。

import time
 
start = time.clock()
 
#当中是你的程序
 
elapsed = (time.clock() - start)
print("Time used:",elapsed)

比如我使用UIAutomator2写自动化脚本时,想知道从启动app到app主界面的时间就可以这样编写:

import time
import uiautomator2 as u2
d = u2.connect()
start = time.clock()
# 启动应用
d.app_start("com.zhongduomei.rrmj.society")
#当前页面出现"精选"文本元素时停止计时
if d(text="精选").wait():
    elapsed = (time.perf_counter() - start)
    print("elapsed",elapsed)

注意当:
startTime = time.clock()AttributeError: module ‘time’ has no attribute 'clock

python 报错!

AttributeError module ‘time’ has no attribute ‘clock’

python3.8不支持clock了,替换成time.perf_counter()替换就可以了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值