[erlang-questions] Erlang shows its slow face!

Hynek Vychodil hynek@REDACTED
Sun Nov 14 10:17:58 CET 2010


On Sun, Nov 14, 2010 at 9:52 AM, Willem de Jong <w.a.de.jong@REDACTED> wrote:
> Hi,
>
> I think the key is to minimize the number of calculations and comparisons.
> In your version you do a lot of multiplications that can easily be avoided.
>
> The version below is about 10 x as fast on my PC:
>
> pythag2(N) ->
>   L = [{A, A*A} || A <- lists:seq(1,N)],
>   lists:flatten([forAllBs(A, A2, L, N) || {A, A2} <- L]).
>
> forAllBs(A, A2, L, N) ->
>  [forAllCs(A, B, A + B, A2 + B2, L, N) || {B, B2} <- L, A + B < N].
>
> forAllCs(A, B, AB, A2B2, L, N) ->
>  [{A, B, C} || {C, C2} <- L, A2B2 =:= C2, AB + C =< N].
>
> Regards,
> Willem
>

Good idea but it seems that native (HiPE) compiler does this
optimization for you when you keep staying in one module. (I also
keeps exporting only main function. It can possibly take effect here
also.) In BEAM it gives you 50% performance gain. Anyway Erlang is not
right tool for this job. You should use NIF if performance matter.

pythag4(N) when is_integer(N) -> pythag4(N,1).

pythag4(N, A) when A+2  > N -> [];
pythag4(N, A) -> pythag4(N, A, A*A, 1).

pythag4(N, A, _A2, B) when A+B+1 > N -> pythag4(N, A+1);
pythag4(N, A, A2, B) -> pythag4(N, A, A2, B, B*B, 1).

pythag4(N, A, A2, B, _B2, C) when A+B+C > N -> pythag4(N, A, A2, B+1);
pythag4(N, A, A2, B, B2, C) when A2 + B2 =:= C*C ->
  [{A, B, C}|pythag4(N, A, A2, B, B2, C+1)];
pythag4(N, A, A2, B, B2, C) -> pythag4(N, A, A2, B, B2, C+1).

> On Sat, Nov 13, 2010 at 8:37 AM, Gilberto Carmenate García <
> co7eb@REDACTED> wrote:
>
>> Hi all!
>> I have been doing tests to Erlang I found this funny stuff that makes
>> Pythagorean Triplets
>>
>> pythag(N) ->
>>    [ {A,B,C} ||
>>        A <- lists:seq(1,N),
>>        B <- lists:seq(1,N),
>>        C <- lists:seq(1,N),
>>        A+B+C =< N,
>>        A*A+B*B =:= C*C].
>>
>> I tested it agains an implementation I made in C# so, and takes 14
>> secounds in my pc to do with 300 numbers in Erlang however in c# is just
>> a secound, even when C# runs under VM too.
>> So I did all possible ways for me to implement differents manners in
>> Erlang looking for speed and all is the same, listed as  follows:
>>
>> So my question is, there are any way to do that even more fast, why 3
>> nestes fors structs in C# are more effients that lists:foldr or
>> lists:foreach in Erlang.
>>
>> %% FORMA 1
>> py1(Max)->
>>    L = lists:seq(1, Max),
>>    lists:foldr(
>>        fun(A, Acc3)->
>>            lists:foldr(
>>                fun(B, Acc2)->
>>                    lists:foldr(
>>                        fun(C, Acc)->
>>                            case ((A*A + B*B =:= C*C) andalso (A+B+C =<
>> Max)) of
>>                                                                true->
>>                                    [{A,B,C}|Acc];
>>                                false->
>>                                    Acc
>>                            end
>>                        end
>>                    , Acc2, L)
>>                end
>>            , Acc3, L)
>>        end
>>    , [], L).
>>
>>
>>
>> %% FORMA 2
>> py2(Max)->
>>        fora(1, [], Max).
>>
>> fora(A, Acc, Max)->
>>        Acc1 = forb(A,1, Acc, Max),
>>        case A < Max of
>>        true->
>>            fora(A+1, Acc1, Max);
>>        false->
>>            Acc1
>>    end.
>>
>> forb(A,B, Acc, Max)->
>>    Acc1 = forc(A,B,1, Acc, Max),
>>    case B < Max of
>>        true->
>>            forb(A,B+1, Acc1, Max);
>>        false->
>>            Acc1
>>    end.
>>
>> forc(A,B,C, Acc, Max)->
>>    Acc1 = case (A*A + B*B =:= C*C) andalso (A+B+C =< Max) of
>>        true->
>>            [{A,B,C}|Acc];
>>        _->
>>            Acc
>>    end,
>>    case C < Max of
>>        true->
>>            forc(A,B,C+1, Acc1, Max);
>>        false->
>>            Acc1
>>    end.
>>
>> %% FORMA 3.
>> py3(Max)->
>>        [{A,B,C} ||
>>                A <-lists:seq(1, Max),
>>                B <-lists:seq(1, Max),
>>                C <-lists:seq(1, Max),
>>                A*A + B*B =:= C*C,
>>                A+B+C =< Max].
>>
>>
>> =======================================================================
>> Este mensaje ha sido enviado mediante el servicio de correo electronico que
>> ofrece la Federacion de Radioaficionados de Cuba a sus miembros para
>> respaldar el cumplimiento de los objetivos de la organizacion y su politica
>> informativa. La persona que envia este correo asume el compromiso de  usar
>> el servicio a tales fines y cumplir con las regulaciones establecidas.
>>
>>
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>
>>
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com


More information about the erlang-questions mailing list