嘿,我下周有个作业,用编译器做基准测试,教授让我们写一个脚本,实际上是命令行。我是python新手,也是在UNIX上编写脚本的新手。如何在Python上编写脚本?我需要教程或任何建议
谢谢
我们必须编写这些步骤
对于每个实验,请执行以下步骤:
For each benchmark directory, change to that directory and compile the code with one of the optimization levels. For example:
cd adpcm
gcc -O0 -o adpcm-O0 adpcm.c
Time the runtime of the executable:
time ./adpcm-O0
Record the “real” time displayed. You might take an average of 3-5 runs to get a stable result.
Use the performance measurement tools
On the Pis:
run rpistat on the executable
e.g. rpistat ./adpcm-O0
that generates a textfile rpistat.txt in the same directory. Record the Cycles and Instructions (use the value in [brackets] for the instruction count).
On the lab workstations:
run perf on the executable
e.g. perf stat ./adpcm-O0
that prints to stdout (you can redirect if you wish). Record the Cycles and Instructions.
Repeat this procedure for all 12 benchmarks and all 4 optimization levels on both machines.
For the fastest version of each benchmark (use lowest cycle count in the case of a tie), profile the application:
gcc -pg -O2 -o adpcm-prof adpcm.c
./adpcm-prof (This is needed to profile the executable, but you don’t need to record the runtime)
gprof ./adpcm-prof | less
Record the function for which the most execution time is spent. 发布于 2014-04-24 09:34:46
subprocess.Popen、os.listdir、os.chdir和time.time()应该会有所帮助。这些都是python命令。例如,你可以用"import subprocess“得到子进程模块,用"import os”得到os模块。
要创建一个Python脚本,只需在*ix上使用您喜欢的文本编辑器编辑一个文本文件(本例中名为"the- script“),内容如下:
#!/usr/bin/python3
# or you can use python 2 with /usr/bin/python
print('hello world')...and使其成为可执行文件:
chmod 755 the-script...then您可以像这样运行它:
./the-scriptHTH
发布于 2014-04-24 09:24:18
要在shell中调用某些内容,请使用以下命令:
>>> import os
>>> os.system('echo hello')
hello
0
>>> 或:
>>> import subprocess
>>> subprocess.call(['echo', 'hello'])
hello
0
>>> 如果要使用诸如ls之类的命令,请使用以下代码:
>>> import os
>>> x = os.popen('ls ~/Desktop').read().split()
>>> x
['...
']我想这就是你的意思,但如果你能在你的问题中添加更多的细节,那就太好了。
https://stackoverflow.com/questions/23258111
复制相似问题