Spawning a process and load a new executable in it is likely to take a few miliseconds, so that kind of precision doesn't really make sense. Also note that CPU time on many systems is allocated to processes by slices of up to 10ms.
Having said that, some sleep
implementations take fractional numbers of seconds, and both zsh and ksh93 can make their $SECONDS
special variable fractional with typeset -F SECONDS
.
Example (zsh):
$ typeset -F SECONDS=0; for ((i=1; i<=70; i++)); do sleep $((1./70)); date +%s.%N; done | { head -n3;echo ..;tail -n3; }; echo $SECONDS
1350076317.374870501
1350076317.391034397
1350076317.407278461
..
1350076318.464585550
1350076318.480887660
1350076318.497133050
1.1393780000
Oops, it drifted. You can adjust the sleeping time based on $SECONDS
:
$ typeset -F SECONDS=0; for ((i=1; i<=70; i++)); do sleep $((i/70. - SECONDS)); date +%s.%N; done | { head -n3;echo ...;tail -n3; }; echo $SECONDS
1350076420.262775654
1350076420.277012997
1350076420.291302750
../..
1350076421.219682227
1350076421.234134663
1350076421.248255685
1.0020580000
Those 2 extra miliseconds are probably to be accounted to running the last sleep
and date
commands.
Also note that zsh has a zselect
builtin with timeout expressed in hundredth of a second. And ksh93 has sleep
built in (and accepts floating points) and its printf
can print date/times.
$ typeset -F SECONDS=0; for ((i=1; i<=70; i++)); do ((i<4 || i>67)) && printf '%(%S.%N)T\n' now; sleep $((i/70.-SECONDS)); done; echo $SECONDS
20.823349000
20.837510000
20.851663000
21.780099000
21.794254000
21.808405000
0.9992358685
If you want anything more precise, you'll probably want a real time operating system or an operating system with real time capabilities and certainly not use a shell.