python - 使用 Python 进行基准测试

标签 python

我希望使用 Python 作为一个系统来对其他进程的时间、数据 I/O、正确性等进行基准测试。我真正感兴趣的是时间的准确性。例如

start = time()
subprocess.call('md5sum', somelist)
end = time()
print("%d s", end-start)

子进程是否会给被调用的函数增加相当大的开销。

编辑

好吧,经过一些快速测试后,我的最佳选择似乎是使用 subprocess 但是我注意到包含 stdout/stderr WITH communicate 调用它添加了大约 0.002338 s额外的执行时间。

最佳答案

使用timeit

import timeit
timeit.timeit('yourfunction')

更新

如果你在 linux 中,那么你可以使用 time 命令,如下所示:

import subprocess
result = subprocess.Popen(['time', 'md5sum', somelist], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()
print result

关于python - 使用 Python 进行基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14905800/

相关文章:

python - Docker错误命令非零代码: 1 python

python - 使用 Pool 在 Python 中进行多重处理

Python lxml : insert text at given position relatively to subelements

python - 测试和 python 包结构

python webkit webview : Why does the history have dates that equal 0. 0,需要修复什么?

python - 我如何编写一段代码来检查用户输入中某个单词出现的次数

python - Bottle.py HTTP 身份验证?

python - 从字典映射中替换字符串中的字符

python - 无法与 GeoDjango 合并范围聚合

Python SQL DB 字符串文字和转义