[erlang-questions] writing a delay loop without now()

Richard Carlsson <>
Wed Feb 18 08:39:08 CET 2009


James Hague wrote:
> I need more resolution than timer:sleep() will give me. (timer:sleep
> gives widely varying results, based on the underlying operating
> system).  Ugly though it may be, I've been using a spin loop based on
> now() and timer:now_diff().  While this appears to work, based on the
> values returned by now(), it runs much faster than wall clock time.
> I'm assuming it's because of this:
> 
> "It is also guaranteed that subsequent calls to this BIF returns
> continuously increasing values."
> 
> Is there another BIF I should be using?  I'm hoping I don't have to
> write my own linked-in driver for this.

Try erlang:statistics(runtime) or erlang:statistics(wall_clock).
The resolution is still milliseconds, but you could perhaps combine
them with a simple loop that you calibrate at startup, for sub-
millisecond delays.

    /Richard



More information about the erlang-questions mailing list