更准确的计时器,用于测量来自 Python 的外部.exe运行时

More accurate timer for measuring external .exe runtime from Python?

本文关键字:Python 外部 exe 运行时 用于 计时器 测量      更新时间:2023-10-16

我正在尝试使用Python从编译C++exe中获取返回值(同时对它们进行计时)。

这是我的蟒蛇代码

import subprocess
import time
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
t1 = time.clock()
h = subprocess.Popen([r"C:UsersMyNameDesktoptest.exe"], startupinfo=info) 
h.communicate()
t2 = time.clock()-t1
print "Return Code:", h.returncode
print "Duration:", t2

这是测试的内容.cpp:

int main(){    
    return 1234;
}

这是蟒蛇的输出

Return Code: 1234
Duration: 0.0438238265388

我觉得 43+ 毫秒太长且不准确。有没有更好的方法?

我还建议从程序本身测量时间C++如果您仍然有兴趣在 Python 程序中获取时间,您可以返回从C++程序计算的时间,这应该是非常准确的。同样正如其他人建议的那样,平均多次运行。