编写用于UNIX命令行的Python脚本
你好,我下周有一个作业,要用编译器做基准测试,教授希望我们写一个脚本,实际上就是命令行。我对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.
3 个回答
0
要在命令行中调用某个东西,可以使用以下代码:
>>> 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
['...
']
我猜这就是你的意思,不过如果你能在问题中提供更多细节,那就更好了。
3
subprocess.Popen、os.listdir、os.chdir 和 time.time() 这些都是 Python 的命令,可以帮助你完成一些任务。你可以通过 "import subprocess" 来引入 subprocess 模块,通过 "import os" 来引入 os 模块,举个例子。
要创建一个 Python 脚本,你只需要用你喜欢的文本编辑器编辑一个文本文件(这里我们叫它 "the-script"),内容可以是这样的:
#!/usr/bin/python3
# or you can use python 2 with /usr/bin/python
print('hello world')
...然后让它可以执行:
chmod 755 the-script
...接着你就可以像这样运行它:
./the-script
希望这些对你有帮助。