0

I'm writing this script where I have to run a program, time it and write that time into a text file, it must be in seconds with 6 decimal places. So far I have this:

#!/bin/bash

MAX_THREADS=8   #2 x number of cores

if [ $# -ne 2 ]; then                  #checks number of arguments
    echo Invalid number of arguments
    exit 1
else
    NUM_THREADS=$1
fi

if [ $NUM_THREADS -gt $MAX_THREADS ]; then     
    echo Invalid number of threads
    exit 1
fi    

make clean
make
start=$(date +%s)
./CircuitRouter-SeqSolver $2
end=$(date +%s)

 cat > $2.speedup.csv << EOF
 $((end-start))
 EOF

You can ignore anything related to NUM_THREADS that's for another part of the script. So I make clean, make and then run CircuitRouter-SeqSolver with $2 as an argument, I saw this solution with date but it only writes the number of seconds without decimal numbers so it just writes 0 into the file since the program takes less than 1 second to finish. How can I modify date to write decimal numbers?

2
  • You could run your sample code, say, 100 times, and divide the total duration by 100 to get a better idea of the time taken.
    – roaima
    Nov 11 '18 at 20:06
  • I'll "leave open" precision might have change in last 7 years.
    – Archemar
    Nov 12 '18 at 8:11
1

Assuming date is the GNU version:

start=$(date +%s.%N)
./CircuitRouter-SeqSolver $2
end=$(date +%s.%N)

Other versions may or may not have %N nanosecond part. To limit the resolution to six decimal places you could do various things, but easiest is likely:

printf "%0.6f\n" $((end-start))

Edit: fix for bash doesn't do floating point math:

printf %0.6f\n" $(echo $end-$start | bc)
4
  • What version of bash supports math with decimals (non-integers) in $((…))? Nov 11 '18 at 6:10
  • Haha. Good question. I don't know. I use ksh and it just worked, and I thought bash would too. Absent that being portable, I'd use bc: duration=$(echo 1.0123456789 - 0.123456789 | bc) Nov 11 '18 at 6:21
  • @CupcakeProtocol But won't this print in nanoseconds? Also, how do I print just the value to the file?
    – MuchoG
    Nov 11 '18 at 18:49
  • You use the printf to round to the detail you want and you use regular shell redirects to print to a file. Judging from your use of a here-doc it seems you know how to do that. Nov 12 '18 at 0:54
-2

Did you have a look at my bosh package from http://schilytools.sourceforge.net/bosh.html? It is the current maintained version of the Bourne Shell.

It supports to do timing with microsecond accuracy, see man the page http://schillix.sourceforge.net/man/man1/bosh.1.html in the parameter section (around page 25) there is the description for the variable TIMEFORMAT.

Note that the accuracy for System CPU time and User CPU time on Linux is only 1 ms, but the wall clock time can be retrieved with microsecond accuracy.

You need to know that the output of the time builtin or automated timing for every command goes to stderr of the main shell and that you may need to redirect stderr for the command separately to be able to fetch the related output in a separate file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.