0

Hey there I have an assignment next week benchmark using compiler and the professor wants us to write an script actually the command lines. I am new on python and writing scripts on UNIX. How do you write a script on Python? I need tutorials or any advice

Thanks

We have to write these steps

For each experiment, follow these steps:

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. 
user2822413
  • 97
  • 4
  • 9
  • None of this seems to require Python at all. Are you sure you are not supposed to be writing a shell script? (In which case you can pretty much comment out the text parts and be done.) – tripleee Apr 24 '14 at 01:35

3 Answers3

3

subprocess.Popen, os.listdir, os.chdir and time.time() should help. These are all python commands. You can get the subprocess module with "import subprocess", and the os module with "import os", for examples.

To create a Python script, just edit a text file (named "the-script" for this example) with your favorite text editor on *ix, with content like:

#!/usr/bin/python3
# or you can use python 2 with /usr/bin/python

print('hello world')

...and make it executable:

chmod 755 the-script

...then you can run it like:

./the-script

HTH

dstromberg
  • 6,954
  • 1
  • 26
  • 27
0

To call something in the shell, use the following:

>>> import os
>>> os.system('echo hello')
hello
0
>>> 

Or:

>>> import subprocess
>>> subprocess.call(['echo', 'hello'])
hello
0
>>> 

If you want to use commands like ls, use the following code:

>>> import os
>>> x = os.popen('ls ~/Desktop').read().split()
>>> x
['...
']

I assume this is what you mean, but if you could add more details to your question that would be great.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • Note that `cd` and `ls` are very different propositions. Running `ls` is fairly straight-forward; you can do what the shell does and execute an external binary (`/bin/ls` or `/usr/bin/ls`). The `cd` command is a shell builtin for the simple and good reason that if it is an external command, the external command changing its own directory has no effect on the parent process's directory. So, a Python script will have to simulate `cd` internally, accessing the `chdir()` system call with `os.chdir()` or one of its relatives. – Jonathan Leffler Apr 24 '14 at 13:40
0

Use the Popen constructor to invoke shell command inside python. Look at this answer

Also, here is another code:

   from subprocess import Popen, PIPE
    
    output = Popen(['ls -l'], stdout=PIPE, stderr=PIPE, shell=True)
    (out,err) = output.communicate()

out contains the stdout from ls -l and err contains the stderr from ls -l

Community
  • 1
  • 1
tomkaith13
  • 1,717
  • 4
  • 27
  • 39