[erlang-questions] list_element()

Henning Diedrich hd2010@REDACTED
Thu May 27 22:27:41 CEST 2010


I did a naive benchmark and it looks that foldl, maybe because of the 
fun() (?) is a lot slower?

Even when looking up the last parameter in a list, when it is not 
aggravated by the fact that it traverses the whole list always.

I thought that may be if interest. Maybe somebody knows the reason?

'if' replaced by pattern matching as you suggested, Ariel.

            2> c(listord).
            2> listord:run().
            *listord1: run 70 wall 153 ms.*
            listord2: run 180 wall 377 ms.
            listord3: run 190 wall 416 ms.
            listord4: run 1240 wall 2181 ms.
            ok


            -module(listord).
            -export([run/0,listOrd3Folder/2]).


            run() ->

                List = [<<"Mabar">>, <<"Mibar">>, <<"Mobar">>,
            <<"Mubar">>, <<"Pabar">>, <<"Pebar">>, <<"Pibar">>,
            <<"Lubar">>, <<"Labar">>, <<"Lebar">>, <<"Libar">> ],
                N = 100000,
                S = <<"Libar">>,

                % . . . . . . . . . . . . . . . . . . . . . . . . . . .
            . . . . . . . . . .

                statistics(runtime),
                statistics(wall_clock),

                for(1,N, fun() -> listOrd1(S, List) end),

                {_, RunTime1} = statistics(runtime),
                {_, WallClock1} = statistics(wall_clock),
               
                io:format("listord1: run ~p wall ~p ms.~n", [RunTime1,
            WallClock1]),

                % . . . . . . . . . . . . . . . . . . . . . . . . . . .
            . . . . . . . . . .


                statistics(runtime),
                statistics(wall_clock),

                for(1,N, fun() -> listOrd2(S, List) end),

                {_, RunTime2} = statistics(runtime),
                {_, WallClock2} = statistics(wall_clock),
               
                io:format("listord2: run ~p wall ~p ms.~n", [RunTime2,
            WallClock2]),

                % . . . . . . . . . . . . . . . . . . . . . . . . . . .
            . . . . . . . . . .

                statistics(runtime),
                statistics(wall_clock),

                for(1,N, fun() -> listOrd3(S, List) end),

                {_, RunTime3} = statistics(runtime),
                {_, WallClock3} = statistics(wall_clock),
               
                io:format("listord3: run ~p wall ~p ms.~n", [RunTime3,
            WallClock3]),

                % . . . . . . . . . . . . . . . . . . . . . . . . . . .
            . . . . . . . . . .

                statistics(runtime),
                statistics(wall_clock),

                for(1,N, fun() -> listOrd4(S, List) end),

                {_, RunTime4} = statistics(runtime),
                {_, WallClock4} = statistics(wall_clock),
               
                io:format("listord4: run ~p wall ~p ms.~n", [RunTime4,
            WallClock4]).

            % . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
            . . . . . . . . . .

            for(N,N,F) -> F();
            for(I,N,F) -> F(), for(I+1,N,F).

            %
            -----------------------------------------------------------------------------

            *listOrd1(Searched, List) when is_list(List) ->

                listOrd1(Searched, List, 1).

            listOrd1(_, [], _) -> nil;

            listOrd1(Searched, [ Searched | _ ], Count) ->

                Count;

            listOrd1(Searched, [ _ | Tail ], Count) ->

                listOrd1(Searched, Tail, Count + 1).*

            %
            -----------------------------------------------------------------------------

            listOrd2(Searched, List) ->
              
                {_,I} = lists:foldl(
                  
                    fun(Item,{Acc,Index})->

                         if
                            (Item==Searched) -> {Acc,Acc};
                            true-> {Acc+1,Index}
                         end
                     end,

                    {1,nil},
                    List),
                I.

            %
            -----------------------------------------------------------------------------

            listOrd3(Searched, List) ->
              
              
                {_,_,I} = lists:foldl(
                  
                    fun listord:listOrd3Folder/2,
                    {Searched,1,nil},
                    List),
                I.
               
            listOrd3Folder(Item,{Searched,Acc,Index})->

                if
                    (Item==Searched) -> {Searched,Acc,Acc};
                    true-> {Searched,Acc+1,Index}
                end.

            %
            -----------------------------------------------------------------------------

            listOrd4(Searched, List) ->
              
              
                L = length(List),
                   hd([ Y || X <- List, Y <- lists:seq(1,L), X ==
            Searched ]).




Ariel Gomez wrote:
> >lists:foldl(fun(Item,{Acc,Index})-> if(Item=="G")-> {Acc,Acc}; true-> 
> {Acc+1,Index} end end,{1,nil},["A","B","C","D","E","F","G","H","I","J"
> ,"K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]).
> {26,7}
>
> 2010/5/27 Henning Diedrich <hd2010@REDACTED 
> <mailto:hd2010@REDACTED>>
>
>     No, look the index up by the content.
>
>     That's a misleading title.
>
>
>     Ivan Uemlianin wrote:
>
>         Dear Henning
>
>         Do you mean you need to access the nth element of a list?  Is
>         lists:nth/2 no good?
>
>            http://www.erlang.org/doc/man/lists.html#nth-2
>
>         Best wishes
>
>         Ivan
>
>         On 27/05/2010 15:31, Henning Diedrich wrote:
>
>             Hi list,
>
>             I need to access a list element by index.
>
>             I wrote a function but it looks most clumsy.
>
>             I am looking for maximal performance. Should I instead
>             convert the list to a tuple and use element()?
>
>             The lists are ~ 10 to 30 elements in length. (<-- not an
>             attempt to define a frame).
>
>             For curiosity, does the below tail-recurse correctly?
>
>                   listOrd(_, []) -> nil;
>
>                   listOrd(Searched, List) when is_list(List) ->
>
>                       listOrd(Searched, List, 1).
>
>                   listOrd(_, [], _) -> nil;
>
>                   listOrd(Searched, [ Element | Tail ], Count) ->
>
>                       case Searched == Element of
>                           true -> Count;
>                           _ -> listOrd(Searched, Tail, Count + 1)
>                       end.
>
>
>
>             Thanks for your time,
>             Henning
>
>
>
>
>



More information about the erlang-questions mailing list