How should I account for subprocess.Popen() overhead when timing in python?

旧城冷巷雨未停 提交于 2019-12-10 16:05:39

问题


more-intelligent-members-of-the-coding-community-than-I! I have a python question for you all... :)

I am trying to optimize a python script that is (among other things) returning the wall-clock time a subprocess took execute and terminate. I think I'm close with something like this.

startTime = time.time()
process = subprocess.Popen(['process', 'to', 'test'])
process.wait()
endTime = time.time()
wallTime = endTime - startTime

However, I am concerned that the overhead in subprocess.Popen() is inflating my results on older and exotic platforms. My first inclination is to somehow time the overhead caused by subprocess.Popen() by running a simple innocuous command. Such as the following on linux.

startTime = time.time()
process = subprocess.Popen(['touch', '/tmp/foo'])
process.wait()
endTime = time.time()
overheadWallTime = endTime - startTime

Or the following on windows.

startTime = time.time()
process = subprocess.Popen(['type', 'NUL', '>', 'tmp'])
process.wait()
endTime = time.time()
overheadWallTime = endTime - startTime

So, how should I account for subprocess.Popen() overhead when timing in python?

Or, what is an innocuous way to sample the overhead from subprocess.Popen()?

Or, maybe there's something I haven't considered yet?

The caveat is that the solution must be reasonably cross-platform.


回答1:


Welcome to batteries included.



来源:https://stackoverflow.com/questions/7560708/how-should-i-account-for-subprocess-popen-overhead-when-timing-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!