[erlang-questions] lists:append performance

Valentin Micic v@REDACTED
Fri Jul 24 15:24:46 CEST 2009


Thanks Vlad,

I've adjusted my benchmark code to avoid optimization, by creating operating
list dynamically:

-export( [
                insert/2               % insert( Count, ListSz )
                ,append/2              % append( Count, ListSz )
         ] ).
         
         
insert( Count, ListSz ) -> do_insert( Count, do_make_list( [], ListSz ) ).
append( Count, ListSz ) -> do_append( Count, do_make_list( [], ListSz ) ).

do_insert( 0, _ )  -> ok;
do_insert( Count, List )
->
    _L = ["1"|List],
    do_insert( Count-1, List )
.

do_append( 0, _ ) -> ok;
do_append( Count, List )
->
    _ = lists:append( List, ["1"] ),
    do_append( Count-1, List )
.

do_make_list( L, 0 ) -> L;
do_make_list( L, ListSz )
->
  do_make_list( ["The quick brown fox jumps over a lzay dog"|L], ListSz-1 ).


Now, I can see a drastic difference in performance:

90> timer:tc( benchmarks, insert, [1000000, 240] ).
{15000,ok}
91> timer:tc( benchmarks, append, [1000000, 240] ).
{2782000,ok}

Thanks for your help.

V.

-----Original Message-----
From: erlang-questions@REDACTED [mailto:erlang-questions@REDACTED] On
Behalf Of Vlad Dumitrescu
Sent: 24 July 2009 03:01 PM
To: erlang-questions
Subject: Fwd: [erlang-questions] lists:append performance

forgot to send to list...


---------- Forwarded message ----------
From: Vlad Dumitrescu <vladdu55@REDACTED>
Date: Fri, Jul 24, 2009 at 15:00
Subject: Re: [erlang-questions] lists:append performance
To: Valentin Micic <v@REDACTED>


On Fri, Jul 24, 2009 at 14:47, Valentin Micic<v@REDACTED> wrote:
> No, lists:reverse/1 is never executed. The goal was to see how slower
> lists:append/2 is. The code looks like indicated below:
>
> insert( 0 ) -> ok;
> insert( Count )
> ->
>   _L = ["1"|?L240],
>   isert( Count - 1 )
> .
>
> append( 0 ) -> ok;
> append( Count )
> ->
>    _ = lists:append( ?L240, ["1"] ),
>    append( Count - 1 )

Aha, okay, now I see.

Compiling with the 'S' option gives this

{function, insert, 1, 2}.
 {label,1}.
   {func_info,{atom,b},{atom,insert},1}.
 {label,2}.
   {test,is_eq_exact,{f,3},[{x,0},{integer,0}]}.
   {move,{atom,ok},{x,0}}.
   return.
 {label,3}.
   {gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,0}}.
   {'%live',1}.
   {call_only,1,{f,2}}.


{function, append, 1, 5}.
 {label,4}.
   {func_info,{atom,b},{atom,append},1}.
 {label,5}.
   {test,is_eq_exact,{f,6},[{x,0},{integer,0}]}.
   {move,{atom,ok},{x,0}}.
   return.
 {label,6}.
   {gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,0}}.
   {'%live',1}.
   {call_only,1,{f,5}}.

which means that the compiler has optimized away both list operations
and the functions are in fact identical!

regards,
Vlad

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org



More information about the erlang-questions mailing list