# [erlang-questions] Erlang/YAWS vs Free Pascal/Xitami

Hynek Vychodil vychodil.hynek@REDACTED
Tue Mar 25 15:12:11 CET 2008

```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.

So I tried improve it a little:

-module(stddev).
-compile([export_all]).

variants(1, N) ->
var_dev(lists:seq(1,N));
variants(2, N) ->
var_dev2(lists:seq(1,N));
variants(3, N) ->
var_dev3(lists:seq(1,N)).

var_dev(V) ->
Len = length(V),
Media = lists:sum(V) / Len,
D = [X-Media || X <- V],
Q = [X*X || X <-D],
S = lists:sum(Q),
Var = S / Len,
_Dev = math:sqrt(Var).

var_dev2(V) ->
{Len, Sum, Sum2} = lists:foldl(
fun(X, {L, S, S2}) -> {L+1, S+X, S2+X*X} end,
{0,0,0},
V
),
Media = Sum/Len,
S = Sum2 - (Media*Media*Len),
Var = S/Len,
_Dev = math:sqrt(Var).

var_dev3(V) ->
Len = length(V),
Sum = lists:sum(V),
Media = Sum/Len,
S = lists:foldl(
fun(X, S) -> D = X-Media, S+D*D end,
0,
V
),
Var = S/Len,
_Dev = math:sqrt(Var).

and measures are:

3> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev,
variants, [X, 10000])]) end, [1,2,3]).
1: {2886,2886.75}
2: {2776,2886.75}
3: {2581,2886.75}
ok
44> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev,
variants, [X, 10000])]) end, [1,2,3]).
1: {2951,2886.75}
2: {2768,2886.75}
3: {2581,2886.75}
ok
45> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev,
variants, [X, 10000])]) end, [1,2,3]).
1: {2776,2886.75}
2: {3132,2886.75}
3: {2481,2886.75}
ok
46> lists:foreach(fun(X)->io:format("~p: ~p~n", [X, timer:tc(stddev,
variants, [X, 10000])]) end, [1,2,3]).
1: {2793,2886.75}
2: {2800,2886.75}
3: {2578,2886.75}
ok

So the third version is best :-) This does not compute with big numbers and
also does not make big vectors in memory.

On Tue, Mar 25, 2008 at 12:40 PM, Ulf Wiger <ulf@REDACTED> wrote:

> It was interesting to see that it actually gave better
> performance as well as being more concise, even
> though I guess the difference lies within the margin
> of error. My expectation was that it would make no
> significant difference, performance-wise.
>
> BR,
> Ulf W
>
> 2008/3/25, Jilani Khaldi <jilani@REDACTED>:
> > > ....which gives us:
> >
> > >
> >  > var_dev(V) ->
> >  >   Len = length(V),
> >  >   Media = lists:sum(V) / Len,
> >  >   D = [X-Media || X <- V],
> >  >   Q = [X*X || X <-D],
> >  >   S = lists:sum(Q),
> >  >   Var = S / Len,
> >  >   Dev = math:sqrt(Var),
> >  >   {Media, D, Q, S, Var, Dev}.
> >  >
> >  > out(A) ->
> >  > Z=lists:seq(1,1000),
> >  > {Media, D, Q, S, Var, Dev} = var_dev(Z),
> >  > T=io_lib:format("Std Dev: ~.6f ", [Dev]),
> >  > {html, T}.
> >
> > Thank you Ulf! I just have updated the article adding your code and the
> >  results for 10000 connections.
> >
> > --
> >
> > ***
> >  Jilani KHALDI
> >  http://www.dotpas.org
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>

--
--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080325/971ca5d6/attachment.htm>
```