Skip to main content
 首页 » 编程设计

python之Q进程执行时间

2025年04月02日16del

我想测量 QProcess 对象的执行时间。

PySide 中是否有用于执行时间测量的内部属性、方法或对象?

目前的方法是使用 time.time() 从外部对其进行测量。

示例代码:

from PySide import QtCore 
import time 
 
p = QtCore.QProcess() 
start_time = time.time() 
p.start('ping -n 5 127.0.0.1 >nul') 
p.waitForFinished(-1) 
end_time = time.time() - start_time 
 
print(end_time) 

请您参考如下方法:

您可以通过以下方式执行此操作。这使用系统 time 命令来获取执行时间。

from PySide import QtCore 
import time 
 
p = QtCore.QProcess() 
p.start('time -p ping -n 5 127.0.0.1 >nul') 
p.waitForFinished(-1) 
stdOut = p.readAllStandardOutput() 
print(stdOut) 
#TODO you will have to regex the stdOut to get the values you want. 

这是另一种方法:

from PySide import QtCore 
import time 
 
timer = QtCore.QTime() 
 
 
def handle_proc_stop(*vargs): 
    procTime = timer.elapsed() 
    print("Process took {} miliseconds".format(procTime)) 
 
p = QtCore.QProcess() 
p.started.connect(timer.start) 
p.finished.connect(handle_proc_stop) 
p.start('ping -n 5 127.0.0.1 >nul') 
p.waitForFinished(-1)