用于从Python测量外部.exe运行时的更准确的计时器?

时间:2012-06-09 05:03:56

标签: c++ python time

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

这是我的Python代码

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:\Users\MyName\Desktop\test.exe"], startupinfo=info) 
h.communicate()
t2 = time.clock()-t1

print "Return Code:", h.returncode
print "Duration:", t2

这是test.cpp的内容:

int main(){    
    return 1234;
}

这是Python输出

Return Code: 1234

Duration: 0.0438238265388

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

1 个答案:

答案 0 :(得分:0)

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