3

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.

JackWM
  • 10,085
  • 22
  • 65
  • 92

1 Answers1

7

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
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
  • 1
    good explanation. then further, how to collect the accumulated time? – JackWM Nov 10 '12 at 21:27
  • @JackWM If by accumulated time you mean the total run time of your script, you do `time ./my_script_name`. If you want to see a stopclock-style update of elapsed time information, see my edited post in a sec. – sampson-chen Nov 10 '12 at 21:29
  • I mean the accumulated time of each part, not total time. – JackWM Nov 10 '12 at 21:31
  • @JackWM if you want the accumulated time of each part (I assume you mean each iteration of the loop), put the logic in the loop in a separate function and invoke the function the loop instead. Then use `time` on the function. – sampson-chen Nov 10 '12 at 21:34
  • Can I create a variable `sum_elapsed_time` and do thing like `sum_elapsed_time += elapsed_time` ? – JackWM Nov 10 '12 at 21:48
  • @JackWM You can; the syntax looks like this: `sum_elapsed_time=$((sum_elapsed_time + elapsed_time))` – sampson-chen Nov 10 '12 at 23:22
  • As you're only working with seconds, you might as well avoid a subshell and a fork to `date`, and, instead, use `$SECONDS`. – gniourf_gniourf Nov 11 '12 at 16:07
  • And a better syntax is `((sum_elapsed_time += elapsed_time))`. – gniourf_gniourf Nov 11 '12 at 16:08