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

Per Hedeland per@REDACTED
Fri Feb 20 22:10:56 CET 2009


James Hague <james.hague@REDACTED> wrote:
>
>>>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
>>
>> milliseconds, right?
>
>No, microseconds.  It's more or less 16 milliseconds between updates.

That's what I meant, just had a temporary misconnect between the weird
custom of sprinkling commas over integers and the equally weird custom
of using a comma for the decimal point.

>I could live with good millisecond accuracy.

That you should definitely be able to get from statistics(wall_clock) -
if you don't, I'll have to say that there is something wrong with your
OS/HW and not with Erlang. See snippet below from a tight-loop that gets
the result of gettimeofday() via os:cmd() every 10 calls to statistics()
(the actual call to statistics() amounts to some 0.2% of the execution
time, the rest is os:cmd() and io:format()).

>>But per my previous message, that should only happen if you manage to
>>call it more than once per microsecond - unless gettimeofday() returns
>>bizarre numbers.
>
>On my MacBook, using a now/0 loop for a delay runs about 25% faster
>than it should, so I'm clearly getting more than one call in per
>microsecond, at least sometimes.

I can't follow that conclusion at all - if you don't know how long a
call takes even on average, I can't see how you can deduce it from an
assumption that the cause of the problems with your loop logic is
microsecond-bumping in now/0 - sounds like circular reasoning to me.
On the other hand laptops frequently do things like varying the CPU
frequency that can confuse applications that try to measure time.

--Per

{13536,0}
{13536,0}
1235162740.330364
{13549,13}
{13549,0}
{13549,0}
{13549,0}
{13550,1}
{13550,0}
{13550,0}
{13550,0}
{13550,0}
{13550,0}
1235162740.344203
{13563,13}
{13563,0}
{13563,0}
{13563,0}
{13563,0}
{13563,0}
{13563,0}
{13564,1}
{13564,0}
{13564,0}
1235162740.357672
{13577,13}
{13577,0}



More information about the erlang-questions mailing list