在进行基准测试时将 subprocess.run(....) 的输出写入 python3.5 中的文件
Getting subprocess.run(....)'s output written to a file in python3.5 while benchmarking
我正在 python 中编写一个基准测试驱动程序,其目的是将一些 C++ 源文件 (.cpp) 作为输入,然后为每个输入文件编译它,使其成为可执行文件 ( .out) 然后 运行 该可执行文件,将一些输入作为命令行参数输入到该可执行文件中,并且当该可执行文件是 运行ning 时,使用 /usr/bin/time
测量它所花费的时间(和所有时间) .
所以,换句话说,这个驱动程序正在做的是试图自动化这个语句(用于测量可执行文件的时间):
/usr/bin/time ./way1.out 10 > way1.output
在此语句中,way1.out
(C++ 程序可执行文件)的输入是 10,C++ 程序的输出被写入 way1.output
,然后将所用时间信息打印到控制台/usr/bin/time
。当然,代替 10(如在该语句中),它是驱动程序作业 运行 此命令用于从 1 到 10^6 的所有数字。驱动程序将为所有输入的 C++ 源文件执行此操作,并将每个源文件的 /usr/bin/time
(对于 1 到 10^6 之间的每个值)的输出写入另一个文件(稍后将解析该文件以获得基准测试结果)那个源代码)。
这是我的driver.py:
#!/usr/bin/env python3.5
import subprocess
import sys
n_limit = 1000000
files_list = ["./src/way1.cpp", "./src/way2.cpp"]
def compile_programs(files_list):
try:
for eachFile in files_list:
subprocess.run(["g++", "-std=c++14", eachFile, "-o", eachFile.split(".")[1].split("/")[2] + ".out"], check=True)
except:
print("Some error occured")
return False
return True
def benchmark(files_list):
if compile_programs(files_list):
print("\n\n Compilation Successful")
else:
raise Exception("Compilation Problem")
print("\n\n Benchmarking started..")
for eachFile in files_list:
current_file_name = eachFile.split(".")[1].split("/")[2]
with open(current_file_name + ".results", 'w') as each_file_bench_results:
for n in range(1, n_limit + 1):
print(" Currently running for n =", n, " for filename:", eachFile)
with open(current_file_name+".output", 'w') as current_output_file:
completed_process = subprocess.run(["/usr/bin/time", "./" + current_file_name + ".out", str(n)], stdout=current_output_file)
each_file_bench_results.write(completed_process.stdout)
subprocess.run(["rm", current_file_name + ".output"])
print()
print("\n\n Benchmarking Complete.. Results files are with '.results' extensions")
if __name__ == "__main__":
if (len(sys.argv) == 1):
print("Using default ./src/way1.cpp and ./src/way2.cpp")
benchmark(files_list)
else:
benchmark(*sys.argv[1:])
所以,我使用了 python3 的 subprocess
模块,并使用了它的 run
方法,即 subprocess.run
这一行:
completed_process = subprocess.run(["/usr/bin/time", "./" + current_file_name + ".out", str(n)], stdout=current_output_file)
C++ 程序接收输入,执行,并将输出写入文件,但是 /usr/bin/time
的输出正在终端 上打印,所以我试过这个:
each_file_bench_results.write(completed_process.stdout)
但是,事实证明,completed_process.stdout是None,所以不会被写入文件,但是如果我把这条语句注释掉,然后 /usr/bin/time
的输出被打印到终端。
所以,我的问题是如何将 /usr/bin/time
的输出写入 each_file_bench_results
?
尝试捕获 STDOUT 和 STDERR:
completed_process = subprocess.run(
["/usr/bin/time", "./" + current_file_name + ".out", str(n)],
stdout=current_output_file, stderr=each_file_bench_results
)
似乎 /usr/bin/time
(至少在我的系统上)部分写入了 STDERR。您还可以使用 subprocess.check_output()
更方便的方法,让您更好地控制接收到的输出。
我正在 python 中编写一个基准测试驱动程序,其目的是将一些 C++ 源文件 (.cpp) 作为输入,然后为每个输入文件编译它,使其成为可执行文件 ( .out) 然后 运行 该可执行文件,将一些输入作为命令行参数输入到该可执行文件中,并且当该可执行文件是 运行ning 时,使用 /usr/bin/time
测量它所花费的时间(和所有时间) .
所以,换句话说,这个驱动程序正在做的是试图自动化这个语句(用于测量可执行文件的时间):
/usr/bin/time ./way1.out 10 > way1.output
在此语句中,way1.out
(C++ 程序可执行文件)的输入是 10,C++ 程序的输出被写入 way1.output
,然后将所用时间信息打印到控制台/usr/bin/time
。当然,代替 10(如在该语句中),它是驱动程序作业 运行 此命令用于从 1 到 10^6 的所有数字。驱动程序将为所有输入的 C++ 源文件执行此操作,并将每个源文件的 /usr/bin/time
(对于 1 到 10^6 之间的每个值)的输出写入另一个文件(稍后将解析该文件以获得基准测试结果)那个源代码)。
这是我的driver.py:
#!/usr/bin/env python3.5
import subprocess
import sys
n_limit = 1000000
files_list = ["./src/way1.cpp", "./src/way2.cpp"]
def compile_programs(files_list):
try:
for eachFile in files_list:
subprocess.run(["g++", "-std=c++14", eachFile, "-o", eachFile.split(".")[1].split("/")[2] + ".out"], check=True)
except:
print("Some error occured")
return False
return True
def benchmark(files_list):
if compile_programs(files_list):
print("\n\n Compilation Successful")
else:
raise Exception("Compilation Problem")
print("\n\n Benchmarking started..")
for eachFile in files_list:
current_file_name = eachFile.split(".")[1].split("/")[2]
with open(current_file_name + ".results", 'w') as each_file_bench_results:
for n in range(1, n_limit + 1):
print(" Currently running for n =", n, " for filename:", eachFile)
with open(current_file_name+".output", 'w') as current_output_file:
completed_process = subprocess.run(["/usr/bin/time", "./" + current_file_name + ".out", str(n)], stdout=current_output_file)
each_file_bench_results.write(completed_process.stdout)
subprocess.run(["rm", current_file_name + ".output"])
print()
print("\n\n Benchmarking Complete.. Results files are with '.results' extensions")
if __name__ == "__main__":
if (len(sys.argv) == 1):
print("Using default ./src/way1.cpp and ./src/way2.cpp")
benchmark(files_list)
else:
benchmark(*sys.argv[1:])
所以,我使用了 python3 的 subprocess
模块,并使用了它的 run
方法,即 subprocess.run
这一行:
completed_process = subprocess.run(["/usr/bin/time", "./" + current_file_name + ".out", str(n)], stdout=current_output_file)
C++ 程序接收输入,执行,并将输出写入文件,但是 /usr/bin/time
的输出正在终端 上打印,所以我试过这个:
each_file_bench_results.write(completed_process.stdout)
但是,事实证明,completed_process.stdout是None,所以不会被写入文件,但是如果我把这条语句注释掉,然后 /usr/bin/time
的输出被打印到终端。
所以,我的问题是如何将 /usr/bin/time
的输出写入 each_file_bench_results
?
尝试捕获 STDOUT 和 STDERR:
completed_process = subprocess.run(
["/usr/bin/time", "./" + current_file_name + ".out", str(n)],
stdout=current_output_file, stderr=each_file_bench_results
)
似乎 /usr/bin/time
(至少在我的系统上)部分写入了 STDERR。您还可以使用 subprocess.check_output()
更方便的方法,让您更好地控制接收到的输出。