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

发布于 2024-12-06 18:16:03 字数 980 浏览 2 评论 0原文

编码社区的成员比我更聪明!我有一个 python 问题要问大家...:)

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

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

但是,我担心 subprocess.Popen() 中的开销会夸大我在较旧和异国平台上的结果。我的第一个倾向是通过运行一个简单的无害命令来计算由 subprocess.Popen() 引起的开销。比如linux下的如下。

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

或者在 Windows 上执行以下操作。

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

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

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

或者,也许还有什么我还没有考虑到的?

需要注意的是,该解决方案必须是合理的跨平台的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2024-12-13 18:16:03

欢迎使用附带电池

Welcome to batteries included.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文