How do I run 100 iterations using a bash shell script? I want to know how long it will take to execute one command (start and end time). I want to keep track which iteratio
You can try with:
for i in {1..100}; do time some_script.sh; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'
time
time
so we can grep itgrep ^real
is to get only the lines starting with "real" in the output of time
sed
is to delete the beginning of the line up to minutes part (in the output of time
)awk
adds to the sum, so that in the end it can output the average, which is the total sum, divided by the number of input records (= NR
)The snippet assumes that the running time of some_script.sh is less than 1 minute, otherwise it won't work at all. Depending on your system, the time
builtin might work differently. Another alternative is to use the time command /usr/bin/time instead of the bash builtin.
Note: This script was extracted from here.
This script is based on the answer from @marian0, but there's no limitation to the running time. Name it timeit.sh
and then do ./timeit.sh 10 ./script
to run ./script
10 times and print the average time. Additional arguments can be added as desired.
#!/bin/bash
for i in `seq 1 $1`; do
time "${@:2}"
done 2>&1 |\
grep ^real |\
sed -r -e "s/.*real\t([[:digit:]]+)m([[:digit:]]+\.[[:digit:]]+)s/\1 \2/" |\
awk '{sum += $1 * 60 + $2} END {print sum / NR}'
You may use seq
in iteration as well:
for i in `seq 1 100`; do ... done
The following script shows one way to do it.
#!/usr/bin/bash
for i in {1..100} ; do
echo =============================
echo "Number $i: $(date +%Y-%m-%d-%H:%M:%S)"
( time ( echo $i ; sleep 1 ) ) 2>&1 | sed 's/^/ /'
done | tee timing.log
It uses the bash
range feature to run 100 iterations of the loop, outputting the loop counter and date.
It then times your command (echo $i ; sleep 1
in this case) and combines standard output and error before nicely formatting it, and sending it to both the terminal and a log file for later analysis.
A smaple run with five iterations:
pax> testprog.sh
=============================
Number 1: 2010-09-16-13:44:19
1
real 0m1.063s
user 0m0.077s
sys 0m0.015s
=============================
Number 2: 2010-09-16-13:44:20
2
real 0m1.056s
user 0m0.030s
sys 0m0.046s
=============================
Number 3: 2010-09-16-13:44:21
3
real 0m1.057s
user 0m0.046s
sys 0m0.030s
=============================
Number 4: 2010-09-16-13:44:22
4
real 0m1.057s
user 0m0.061s
sys 0m0.031s
=============================
Number 5: 2010-09-16-13:44:23
5
real 0m1.057s
user 0m0.046s
sys 0m0.015s
for ((i = 1; i <= 100; i++)); do
echo "--- Iteration #$i: $(date) ---"
time command1
done 2>&1 | tee timing.log
There's a lot going on here. What's happening?
for
loop iterates from 1 to 100 using C-style syntax.$i
in the echo printout prints the current iteration number.$(date)
inserts a timestamp into each printout.time
command runs a command and prints how long it took to execute.tee
, which saves a copy to timing.log
.2>&1
redirects stderr to stdout so that the log file will contain both regular output and error messages.