<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hello,</div><div class=""><br class=""></div><div class="">timer:sleep() is implemented as receive after T -> ok end. It does not give you microsecond precision so that error is aggregated. </div><div class=""><br class=""></div><div class="">secondly, <font face="monospace, monospace" class="">lists:foreach(SleepFun, lists:seq(1, Num)) does not look performance friendly for big Num </font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">try tail recursion it gives better results.</font></div><div class=""><br class=""></div><div class="">sleep_loop(0, _) -><br class=""> ok;<br class="">sleep_loop(N, IntervalMs) -><br class=""> receive after IntervalMs -> ok end,<br class=""> sleep_loop(N - 1, IntervalMs).</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">- Dmitry</div><br class=""><div><blockquote type="cite" class=""><div class="">On 12 Jan 2015, at 12:40, Roberto Ostinelli <<a href="mailto:roberto@widetag.com" class="">roberto@widetag.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Dear list,<div class="">I've probably missed out something since I'm not understanding what is going here.</div><div class=""><br class=""></div><div class="">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" class="">timer</font> has some erratic behavior.</div><div class=""><br class=""></div><div class="">I've stripped out the code to the bare minimum and it looks like this:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">-module(test).</font></div><div class=""><font face="monospace, monospace" class="">-export([sleep_periodical/2]).</font></div><div class=""><font face="monospace, monospace" class="">-export([sleep_loop/2]).</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">sleep_periodical(Num, IntervalMs) -></font></div><div class=""><font face="monospace, monospace" class=""><span class="" style="white-space:pre"> </span>{Time, _Value} = timer:tc(?MODULE, sleep_loop, [Num, IntervalMs]),</font></div><div class=""><font face="monospace, monospace" class=""><span class="" style="white-space:pre"> </span>io:format("Finished in ~p microseconds.~n", [Time]).</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">sleep_loop(Num, IntervalMs) -></font></div><div class=""><font face="monospace, monospace" class=""><span class="" style="white-space:pre"> </span>SleepFun = fun(_) -> timer:sleep(IntervalMs) end,</font></div><div class=""><font face="monospace, monospace" class=""><span class="" style="white-space:pre"> </span>lists:foreach(SleepFun, lists:seq(1, Num)).</font></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">When I run this code, I see the following:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">Erlang/OTP 17 [erts-6.3] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">Eshell V6.3 (abort with ^G)</font></div><div class=""><font face="monospace, monospace" class="">1> c(test).</font></div><div class=""><font face="monospace, monospace" class="">{ok,test}</font></div><div class=""><font face="monospace, monospace" class="">2> test:sleep_periodical(1000, 10).</font></div><div class=""><font face="monospace, monospace" class="">Finished in 12257397 microseconds.</font></div><div class=""><font face="monospace, monospace" class="">ok</font></div><div class=""><font face="monospace, monospace" class="">3> test:sleep_periodical(2000, 10).</font></div><div class=""><font face="monospace, monospace" class="">Finished in 24518070 microseconds.</font></div><div class=""><font face="monospace, monospace" class="">ok</font></div><div class=""><font face="monospace, monospace" class="">4> test:sleep_periodical(10000, 1). </font></div><div class=""><font face="monospace, monospace" class="">Finished in 20000280 microseconds.</font></div><div class=""><font face="monospace, monospace" class="">ok</font></div><div class=""><font face="monospace, monospace" class="">5> test:sleep_periodical(20000, 1).</font></div><div class=""><font face="monospace, monospace" class="">Finished in 40000685 microseconds.</font></div><div class=""><font face="monospace, monospace" class="">ok</font></div></div><div class=""><br class=""></div><div class="">So:</div><div class=""><ul class=""><li class="">Looping 1,000 times and sleeping 10 ms each time takes 12.26 seconds (instead of 10).</li><li class="">Looping 2,000 times and sleeping 10 ms each time takes 24.52 seconds (instead of 20).</li><li class="">Looping 10,000 times and sleeping 1 ms each time takes 20.00 seconds (instead of 10).</li><li class="">Looping 20,000 times and sleeping 1 ms each time takes 40.00 seconds (instead of 20).</li></ul><div class="">Up to 10,000 times it takes 12,16% (1/8) more time, from 10,000 times it takes 100% (double).</div></div><div class=""><br class=""></div><div class="">Can some kind soul explain what I'm doing wrong here and how to achieve the desired results?</div><div class=""><br class=""></div><div class="">I'm on OSX, Erlang 17.4.</div><div class=""><br class=""></div><div class="">Help! ^^_</div><div class="">r.</div><div class=""><br class=""></div></div>
_______________________________________________<br class="">erlang-questions mailing list<br class=""><a href="mailto:erlang-questions@erlang.org" class="">erlang-questions@erlang.org</a><br class="">http://erlang.org/mailman/listinfo/erlang-questions<br class=""></div></blockquote></div><br class=""></body></html>