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

tsuraan tsuraan@REDACTED
Mon Feb 23 23:42:15 CET 2009


> -module(test).
> -compile(export_all).
>
> time(N) -> time(N, now()).
> time(N, Start) ->
> 	case timer:now_diff(now(), Start) of
> 		M when M >= N -> M;
> 		_ -> time(N, Start)
> 	end.

My results, on Linux 2.6.28 (vanilla) and a core2 duo at 3GHz:

Erlang (BEAM) emulator version 5.6.1 [source] [smp:2]
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.1  (abort with ^G)
1> c(test).
{ok,test}
2> erlang:statistics(wall_clock),test:time(5000000),erlang:statistics(wall_clock).
{24784,3960}
3> erlang:statistics(wall_clock),test:time(5000000),erlang:statistics(wall_clock).
{30248,3964}
4> c(test, native).
         {ok,test}
5> erlang:statistics(wall_clock),test:time(5000000),erlang:statistics(wall_clock).
{39371,3773}
6> erlang:statistics(wall_clock),test:time(5000000),erlang:statistics(wall_clock).
{44229,3797}

Native does sleep for 5 seconds a bit faster than without native, but
both ways sleep for 5 seconds in noticeably less than 5 seconds.



More information about the erlang-questions mailing list