本文介绍了在python中计时时,应该如何考虑subprocess.Popen()的开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码社区的智能成员比我多!我对所有人都有一个python问题...:)

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

我正在尝试优化一个python脚本,该脚本(除其他外)返回挂钟时间a子进程执行并终止。我想我已经接近了这样的事情。

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

但是,我担心 subprocess.Popen()在旧的和异国情调的平台上提高了我的结果。我的第一个倾向是通过运行一个简单的无害命令,以某种方式确定由 subprocess.Popen()引起的开销。在Linux上如下所示。

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

或在Windows上执行以下操作。

Or the following on windows.

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

所以,我该如何考虑在python中计时时 subprocess.Popen()开销?

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

或者,什么是无开销的采样开销的方法?来自 subprocess.Popen()

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.

推荐答案

欢迎使用。

这篇关于在python中计时时,应该如何考虑subprocess.Popen()的开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:53