Reputation: 10565
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.
Upvotes: 3
Views: 2953
Reputation: 47367
You can prefix the command you wish to time with the appropriately named time
command.
For example:
time find . -type f -name hello_world.cc
Or in your case:
time head -$((i+step)) $1 | tail -$step > tmp1
time head -$((i+step)) $2 | tail -$step > tmp2
time setstr=$setstr' '`./accuracy.sh tmp1 tmp2`
Note that time outputs to tty
, so you don't have to worry about the results of timing being written to tmp1
or tmp2
etc.
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:
start_timestamp=$(date +%s)
Then start your actual script main:
# Do your execution here
Then within your loops, wherever you'd like to see output of elapsed time so far
curr_timestamp=$(date +%s)
elapsed_time=$(expr $end_time - $start_time)
echo "Elapsed: $elapsed_time" >> elapsed_time.log
Upvotes: 7