How to get the runtime of a program with shell script?

Asked

Viewed 445 times

0

I am creating a basic script in which I would like to take the runtime of a program each time it is called in the script.

#!/bin/bash

for i in {1..15}
do
 echo "Execucao $i"
 time ./meu_programa > resultados_nesse_arquivo.txt
done

But when running this script, I get only the default output of my program, how can I get similar result as:

 Execucao X

 real    0m0,001s
 user    0m0,001s
 sys     0m0,000s
  • I don’t quite understand, you want to register the timetable where it initiates the process or the duration of time that the process was being carried out?

1 answer

2


This is because, by default, the team sends the result to stderr instead of stdout.

You just need to redirect the output.

Example:

#!/bin/bash

outfile="benchmark.txt"

for i in $(seq 1 15)
do
    echo "Execução: $i" >> $outfile
    echo "-----------------" >> $outfile
    (time cat arquivo.txt) >> $outfile 2>&1
    echo "" >> $outfile
done

Archive output:

Execução: 1
-----------------
teste1
teste2

real    0m0,003s
user    0m0,003s
sys     0m0,000s

Execução: 2
-----------------
teste1
teste2

real    0m0,003s
user    0m0,004s
sys     0m0,000s

Execução: 3
-----------------
teste1
teste2

real    0m0,003s
user    0m0,003s
sys     0m0,000s

...

Browser other questions tagged

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