This result is unexpected for me and I'm glad to see that erlang performs so good in this computation task. Anyway if you see more precisely what pascal version does, this is unfair to erlnag still :-) Your erlang version makes three vectors in memory but pascal version doesn't.<br>
<br>So I tried improve it a little:<br><br>-module(stddev).<br>-compile([export_all]).<br><br>variants(1, N) -><br> var_dev(lists:seq(1,N));<br>variants(2, N) -><br> var_dev2(lists:seq(1,N));<br>variants(3, N) -><br>
var_dev3(lists:seq(1,N)).<br><br>var_dev(V) -><br> Len = length(V),<br> Media = lists:sum(V) / Len,<br> D = [X-Media || X <- V],<br> Q = [X*X || X <-D],<br> S = lists:sum(Q),<br> Var = S / Len,<br> _Dev = math:sqrt(Var).<br>
<br>var_dev2(V) -><br> {Len, Sum, Sum2} = lists:foldl(<br> fun(X, {L, S, S2}) -> {L+1, S+X, S2+X*X} end,<br> {0,0,0},<br> V<br> ),<br> Media = Sum/Len,<br> S = Sum2 - (Media*Media*Len),<br>
Var = S/Len,<br> _Dev = math:sqrt(Var).<br><br>var_dev3(V) -><br> Len = length(V),<br> Sum = lists:sum(V),<br> Media = Sum/Len,<br> S = lists:foldl(<br> fun(X, S) -> D = X-Media, S+D*D end,<br>
0,<br> V<br> ),<br> Var = S/Len,<br> _Dev = math:sqrt(Var).<br><br>and measures are:<br><br>3> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev, variants, [X, 10000])]) end, [1,2,3]).<br>
1: {2886,2886.75}<br>2: {2776,2886.75}<br>3: {2581,2886.75}<br>ok<br>44> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev, variants, [X, 10000])]) end, [1,2,3]).<br>1: {2951,2886.75}<br>2: {2768,2886.75}<br>
3: {2581,2886.75}<br>ok<br>45> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev, variants, [X, 10000])]) end, [1,2,3]).<br>1: {2776,2886.75}<br>2: {3132,2886.75}<br>3: {2481,2886.75}<br>ok<br>
46> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev, variants, [X, 10000])]) end, [1,2,3]).<br>1: {2793,2886.75}<br>2: {2800,2886.75}<br>3: {2578,2886.75}<br>ok<br><br>So the third version is best :-) This does not compute with big numbers and also does not make big vectors in memory.<br>
<br><div class="gmail_quote">On Tue, Mar 25, 2008 at 12:40 PM, Ulf Wiger <<a href="mailto:ulf@wiger.net">ulf@wiger.net</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It was interesting to see that it actually gave better<br>
performance as well as being more concise, even<br>
though I guess the difference lies within the margin<br>
of error. My expectation was that it would make no<br>
significant difference, performance-wise.<br>
<div class="Ih2E3d"><br>
BR,<br>
Ulf W<br>
<br>
2008/3/25, Jilani Khaldi <<a href="mailto:jilani@cheapnet.it">jilani@cheapnet.it</a>>:<br>
</div><div><div></div><div class="Wj3C7c">> > ....which gives us:<br>
><br>
> ><br>
> > var_dev(V) -><br>
> > Len = length(V),<br>
> > Media = lists:sum(V) / Len,<br>
> > D = [X-Media || X <- V],<br>
> > Q = [X*X || X <-D],<br>
> > S = lists:sum(Q),<br>
> > Var = S / Len,<br>
> > Dev = math:sqrt(Var),<br>
> > {Media, D, Q, S, Var, Dev}.<br>
> ><br>
> > out(A) -><br>
> > Z=lists:seq(1,1000),<br>
> > {Media, D, Q, S, Var, Dev} = var_dev(Z),<br>
> > T=io_lib:format("Std Dev: ~.6f ", [Dev]),<br>
> > {html, T}.<br>
><br>
> Thank you Ulf! I just have updated the article adding your code and the<br>
> results for 10000 connections.<br>
><br>
> --<br>
><br>
> ***<br>
> Jilani KHALDI<br>
> <a href="http://www.dotpas.org" target="_blank">http://www.dotpas.org</a><br>
><br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil