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

James Hague james.hague@REDACTED
Fri Feb 20 00:10:32 CET 2009


Well, I didn't really mean to get into criticizing the design
decisions behind now/0.   Let me rewind a bit.

I have a graphical application that runs at a fixed frame rate (60
frames per second).  The processing takes up part of the frame, then I
need to make sure that roughly 16,667 microseconds have passed before
displaying the next frame.  os:sleep() is VERY coarse and inconsistent
across platforms.  On some platforms os:sleep(1) is the same as
sleep:(32000).  Using a timeout value with receive gives the same
results (that's actually how os:sleep is implemented).
erlang:statistics(wall_clock) is just as coarse.

So get around this, I resorted to just spinning in a tight loop using
now/0 and timer:now_diff/2.  But now/0 isn't designed to be used like
this, because it always returns increasing values.

This would have been so easy on an 8-bit system from 25 years ago, so
surely there's a way to get raw microsecond time values in Erlang?



More information about the erlang-questions mailing list