Skip to main content
3 of 6
added 854 characters in body
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

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 slipped. 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

Also note that zsh has a zselect builtin with timeout expressed in hundredth of a second.

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.

Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k