Bash has a built-in time
command which is used in your case.
The time
described in man time
(usually installed under /usr/bin/time
) can be called as follows
command time -f "%S" ./main
it is too fast, so I need more decimal precision.
I would rather see: 0.000015
Neither bash's time
nor GNU time
seem to be able to output times with such high precision. And if you really think about it there is no need to. There is always background noise and starting a process takes time too. If you measure such short time intervals you mostly measure anything but the time your program actually takes.
If you really have to, you could measure the wall clock time in bash
5 ✱:
t=$EPOCHREALTIME
./main
t="$EPOCHREALTIME - $t" # measure time before starting bc
bc <<< "$t"
However, it would be better to do this directly inside your program. If you could repeat your actual program 1000 times there and measure the complete time you would get a somewhat usable measurement.
✱ On my system measuring with bash added around 0.000008 s of overhead. The overhead was measured with
$ for i in {1..10000}; do t=$EPOCHREALTIME; t="$EPOCHREALTIME - $t"; bc <<< "$t"; done | sponge | datamash min 1 median 1 max 1
4e-06 8e-06 8.1e-05