如何从 python 脚本对 C 程序进行基准测试?
How to benchmark a C program from a python script?
我目前正在 uni 中做一些工作,需要为多个短 C 程序生成多个基准。我编写了一个 python 脚本来自动执行此过程。到目前为止,我一直在使用 time
模块,并且基本上是这样计算基准的:
start = time.time()
successful = run_program(path)
end = time.time()
runtime = end - start
其中 run_program
函数仅使用 subprocess
模块到 运行 C 程序:
def run_program(path):
p = subprocess.Popen(path, shell=True, stdout=subprocess.PIPE)
p.communicate()[0]
if (p.returncode > 1):
return False
return True
不过,我最近发现这种方法测量的是经过的时间,而不是 CPU 时间,即这种测量方法对来自 OS 的噪音很敏感。 SO 上的类似问题表明 timeit
模块更适合测量 CPU 时间,因此我采用了 运行 方法:
def run_program(path):
command = 'p = subprocess.Popen(\'time ' + path + '\', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE); out, err = p.communicate()'
result = timeit.Timer(command, setup='import subprocess').repeat(1, 10)
return numpy.median(result)
但是通过查看 timeit
文档,似乎 timeit
模块仅适用于作为字符串传入的 python 代码的小片段。所以我不确定 timeit
是否为我提供了此计算的准确结果。所以我的问题是:将 timeit
测量它 运行 过程的每个步骤的 CPU 还是只测量实际 [=31] 的 CPU 时间=](即 subprocess
模块)代码到 运行?这是对一组 C 程序进行基准测试的准确方法吗?
timeit
将测量其运行的 Python 进程使用的 CPU 时间。外部进程的执行时间不会"credited"到那些时间。
更准确的方法是在 C 中执行此操作,您可以获得真正的速度和吞吐量。
我目前正在 uni 中做一些工作,需要为多个短 C 程序生成多个基准。我编写了一个 python 脚本来自动执行此过程。到目前为止,我一直在使用 time
模块,并且基本上是这样计算基准的:
start = time.time()
successful = run_program(path)
end = time.time()
runtime = end - start
其中 run_program
函数仅使用 subprocess
模块到 运行 C 程序:
def run_program(path):
p = subprocess.Popen(path, shell=True, stdout=subprocess.PIPE)
p.communicate()[0]
if (p.returncode > 1):
return False
return True
不过,我最近发现这种方法测量的是经过的时间,而不是 CPU 时间,即这种测量方法对来自 OS 的噪音很敏感。 SO 上的类似问题表明 timeit
模块更适合测量 CPU 时间,因此我采用了 运行 方法:
def run_program(path):
command = 'p = subprocess.Popen(\'time ' + path + '\', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE); out, err = p.communicate()'
result = timeit.Timer(command, setup='import subprocess').repeat(1, 10)
return numpy.median(result)
但是通过查看 timeit
文档,似乎 timeit
模块仅适用于作为字符串传入的 python 代码的小片段。所以我不确定 timeit
是否为我提供了此计算的准确结果。所以我的问题是:将 timeit
测量它 运行 过程的每个步骤的 CPU 还是只测量实际 [=31] 的 CPU 时间=](即 subprocess
模块)代码到 运行?这是对一组 C 程序进行基准测试的准确方法吗?
timeit
将测量其运行的 Python 进程使用的 CPU 时间。外部进程的执行时间不会"credited"到那些时间。
更准确的方法是在 C 中执行此操作,您可以获得真正的速度和吞吐量。