Fwd: [erlang-questions] lists:append performance

Vlad Dumitrescu vladdu55@REDACTED
Fri Jul 24 15:00:56 CEST 2009


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


More information about the erlang-questions mailing list