<p dir="ltr">timer:sleep is not even remotely as accurate for those low numbers. It can not go lower than 20ms or so from what i remember.</p>
<p dir="ltr">I have had this issue once when I needed low interval for timer. I used an external c app which printed to stdout every 5ms. I dont think there is any way in erlang to get a timer that low.<br></p>
<p dir="ltr">Sergej</p>
<div class="gmail_quote">On Jan 12, 2015 11:46 AM, "Roberto Ostinelli" <<a href="mailto:roberto@widetag.com">roberto@widetag.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Additionally:<div><br></div><div><div><font face="monospace, monospace">6> test:sleep_periodical(1000, 1). </font></div><div><font face="monospace, monospace">Finished in 1999097 microsecond</font></div></div><div><br></div><div>So:</div><div><ul><li>Looping 1,000 times and sleeping 1 ms each time takes 2.00 seconds (instead of 1).</li></ul><div>Looks like it's ms related, not total count.</div></div><div><br></div><div>Best,</div><div>r.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 12, 2015 at 11:40 AM, Roberto Ostinelli <span dir="ltr"><<a href="mailto:roberto@widetag.com" target="_blank">roberto@widetag.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear list,<div>I've probably missed out something since I'm not understanding what is going here.</div><div><br></div><div>I'm basically just trying to send messages periodically, though I found out that I wasn't seeing the behavior that I was expecting. It looks like the <font face="monospace, monospace">timer</font> has some erratic behavior.</div><div><br></div><div>I've stripped out the code to the bare minimum and it looks like this:</div><div><br></div><div><div><font face="monospace, monospace">-module(test).</font></div><div><font face="monospace, monospace">-export([sleep_periodical/2]).</font></div><div><font face="monospace, monospace">-export([sleep_loop/2]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">sleep_periodical(Num, IntervalMs) -></font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>{Time, _Value} = timer:tc(?MODULE, sleep_loop, [Num, IntervalMs]),</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>io:format("Finished in ~p microseconds.~n", [Time]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">sleep_loop(Num, IntervalMs) -></font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>SleepFun = fun(_) -> timer:sleep(IntervalMs) end,</font></div><div><font face="monospace, monospace"><span style="white-space:pre-wrap"> </span>lists:foreach(SleepFun, lists:seq(1, Num)).</font></div></div><div><br></div><div><br></div><div>When I run this code, I see the following:</div><div><br></div><div><div><font face="monospace, monospace">Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">Eshell V6.3 (abort with ^G)</font></div><div><font face="monospace, monospace">1> c(test).</font></div><div><font face="monospace, monospace">{ok,test}</font></div><div><font face="monospace, monospace">2> test:sleep_periodical(1000, 10).</font></div><div><font face="monospace, monospace">Finished in 12257397 microseconds.</font></div><div><font face="monospace, monospace">ok</font></div><div><font face="monospace, monospace">3> test:sleep_periodical(2000, 10).</font></div><div><font face="monospace, monospace">Finished in 24518070 microseconds.</font></div><div><font face="monospace, monospace">ok</font></div><div><font face="monospace, monospace">4> test:sleep_periodical(10000, 1). </font></div><div><font face="monospace, monospace">Finished in 20000280 microseconds.</font></div><div><font face="monospace, monospace">ok</font></div><div><font face="monospace, monospace">5> test:sleep_periodical(20000, 1).</font></div><div><font face="monospace, monospace">Finished in 40000685 microseconds.</font></div><div><font face="monospace, monospace">ok</font></div></div><div><br></div><div>So:</div><div><ul><li>Looping 1,000 times and sleeping 10 ms each time takes 12.26 seconds (instead of 10).</li><li>Looping 2,000 times and sleeping 10 ms each time takes 24.52 seconds (instead of 20).</li><li>Looping 10,000 times and sleeping 1 ms each time takes 20.00 seconds (instead of 10).</li><li>Looping 20,000 times and sleeping 1 ms each time takes 40.00 seconds (instead of 20).</li></ul><div>Up to 10,000 times it takes 12,16% (1/8) more time, from 10,000 times it takes 100% (double).</div></div><div><br></div><div>Can some kind soul explain what I'm doing wrong here and how to achieve the desired results?</div><div><br></div><div>I'm on OSX, Erlang 17.4.</div><div><br></div><div>Help! ^^_</div><span><font color="#888888"><div>r.</div><div><br></div></font></span></div>
</blockquote></div><br></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div>