How should I account for subprocess.Popen() overhe

2019-06-24 12:38发布

问题:

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.