In Java program, I usually use the following functions to profile time info:
...
long start = System.currentTimeMillis();
...
...
System.out.println("elapsed time 1: "+(System.currentTimeMills() - start));
...
...
start = System.currentTimeMillis();
...
System.out.println("elapsed time 2: "+(System.currentTimeMills() - start));
...
How to do similar things in shell bash? Further, how to collect the accumulated time if there is a loop?
e.g.
for ((i=0;i<$lines;i=i+$step))
do
head -$((i+step)) $1 | tail -$step > tmp1
head -$((i+step)) $2 | tail -$step > tmp2
setstr=$setstr' '`./accuracy.sh tmp1 tmp2`
done
echo $setstr | awk '{for (i=1;i<=NF;i++) sum+=$i; }END{print sum/NF}'
I want to profile the accumulated head/tail time and accuracy.sh tmp1 tmp2 time separately.
You can prefix the command you wish to time with the appropriately named
timecommand.For example:
Or in your case:
Note that time outputs to
tty, so you don’t have to worry about the results of timing being written totmp1ortmp2etc.If you’d like to watch the total elapsed time (since the script has started running) update in real time, you can do this:
At the start of your script, take note of the system time:
Then start your actual script main:
Then within your loops, wherever you’d like to see output of elapsed time so far