I am trying to use Python to grab return values from compiled C++ exe’s (while timing them).
This is my Python code
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
This is the contents of test.cpp:
int main(){
return 1234;
}
This is the Python output
Return Code: 1234
Duration: 0.0438238265388
I feel like 43+ milliseconds is way too long and inaccurate. Is there a better way?
I would also suggest to measure the time from C++ program itself and if your are still interested in getting the time in Python program, you can return back the time calculated from C++ program which should be pretty much accurate. Also as others suggested, take an average of multiple runs.