Linux running average

Asked

Viewed 68 times

0

I need to take the average running time and consumption of CPU de 30 execuções of a program made in Python. I have to use the remote time of Linux. Like I’ve never worked with Linux before I’m having trouble doing this.

This is the code I’ve made so far (not working). Could someone help me make it work?

size=400
n_cpu=4

/usr/bin/time -f "CPU:  %p    TIME:  %e"
for i in {1..30};
do
    python "script.py" $size $size $n_cpu
done 

2 answers

1

python -m timeit -n 30 script.py <…parâmetros…>

The timeit is a standard Python module, which protects you from all pranks associated with timing Python code (e.g. when you change the code, Python "recompiles" the code you wrote; this time is discounted by timeit but not by Bash).

  • This also works with Jython, Ironpython and Pypy?

  • I have no idea, but the module is written in pure Python; the module that can cause you more problems (because it is unavailable) is the gc, exposing low-level Python features.

  • You are giving this error: Nameerror: global name 'ex1_threading' is not defined

  • So it doesn’t work. :(

1

To get just the time, you can use date

t0=$(date +%s)
for i in {1..5};
do
    sleep 1;
done
tf=$(date +%s)
echo "tempo de execução: $((tf - t0))"

To use the team the way you tried, you need to put the loop in a new section of bash with the -c parameter

/usr/bin/time -f "CPU:  %p    TIME:  %e" bash -c "
for i in {1..5};
do
    sleep 1;
done "
  • 1

    @user25946: Use the time for microbenchmarks like the ones you seem to be doing is not ideal for the reasons I mentioned - you are measuring things other than code execution time - but if you are using interpreters other than Cpython (or comparing the different interpreters) the @Marcos solution is correct.

  • Exactly. This is a bad way to benchmark. I put the answer because of the phrase "I have to use the Linux time command".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.