Im trying to write a script so that I can run my program 100 times and grep the execution time. Currently I have this:
#!/bin/bash
i=1
while [ $i -lt 100 ]
do
(time -p ./mdu -j $i /pkg) &>> tempfile.txt
echo $i
let i=$i 1
done
cat tempfile.txt | grep real > plot.txt
However this does not give me the desired output. I wish to only get the execution time of the 100 runs but instead plot.txt comes out with 551 lines of data. All the lines does contain "real 1.31(random time)" but it feels like i get alot of duplicates or something.
What am i doing wrong here?
CodePudding user response:
As you wrote it, you append to tempfile.txt
the resource use statistics, plus the standard and error outputs of your program. So if these last two streams contain real
they will pollute your results. Try:
time (<your-program> &>> log.txt) &>> tempfile.txt
But why don't you use the time
utility, instead of the bash builtin, and its options (time -pao tempfile.txt
) to append only the statistics to your file? Something like:
#!/bin/bash
i=1
while [ $i -lt 100 ]
do
/usr/bin/time -pao tempfile.txt ./mdu -j $i /pkg &>> log.txt
echo $i
let i=$i 1
done
grep "real" tempfile.txt > plot.txt
Moreover, if you use the time
utility you can even specify the output format, instead of using the -p
POSIX format and grep the output. If, for your plot, it is more convenient to have only the real time value in [hours:]minutes:seconds:
/usr/bin/time -f'%E' -ao tempfile.txt ...
And if you want it in seconds:
/usr/bin/time -f'%e' -ao tempfile.txt ...
CodePudding user response:
You may have 551 lines of data because you kept appending to tempfile.txt
and never deleted its contents.
As a side note, your loop runs 99 times, not 100 times. Use either zero-based numbering or <=
.
Last but not least, time
is quite tricky (in its built-in form), especially due to the way it interacts with pipes.
run_n_times() {
local -ir N="$1"
shift
local -ar args=("$@")
local -i i
for ((i = 1; i <= N; i)); do
( time -p "${args[@]//#/"$i"}"; ) \
|& awk '/^real/ {print $2}'
done
}
# A simple test:
run_n_times 3 sleep '#'
# The real thing:
run_n_times 100 ./mdu -j '#' /pkg