在 python 中计时时,我应该如何考虑 subprocess.Popen() 开销?
编码社区的成员比我更聪明!我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
欢迎使用附带电池。
Welcome to batteries included.