[erlang-questions] lists:append performance

Valentin Micic <>
Fri Jul 24 14:47:04 CEST 2009


No, lists:reverse/1 is never executed. The goal was to see how slower
lists:append/2 is. The code looks like indicated below:

-module( benchmarks ).    

% --------------------
% List of 240 elements
% --------------------
-define ( L240,  [ "The quick brown fox jumps over a lazy dog",
                   "The quick brown fox jumps over a lzay dog",
                   ...
                   ...
                   "The quick brown fox jumps over a lzay dog"] ).

export( [
                split_test/1           % split_test( Count ),
                ,reverse_test/1        % reverse_test( Count )
                ,insert/1              % insert( Count )
                ,append/1              % append( Count )
         ] ).

...
...

insert( 0 ) -> ok;
insert( Count )
->
   _L = ["1"|?L240],
   isert( Count - 1 )
.

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

I've tried to measure how long it takes to execute a single operation. Did
it using a substantial number of operations (Count=1000000000) so it can be
measured.

V.


-----Original Message-----
From: Vlad Dumitrescu [mailto:] 
Sent: 24 July 2009 02:33 PM
To: Valentin Micic
Cc: Erlang Users' List
Subject: Re: [erlang-questions] lists:append performance

Hi,

On Fri, Jul 24, 2009 at 14:20, Valentin Micic<> wrote:
> I've always been under an impression that inserting element into the list
> and then reversing the list to convert it from LIFO to FIFO would
generally
> be faster then using lists:append/2 to create FIFO immediately. I

> 1) insert (Count) that would always execute an insert like this:
> ["1"|OldList]

Do you mean that insert does the above and then reverts the list, every
time?

If yes, that's the explanation (reversing takes a little longer than
traversing the list)

If no, then it would be interesting to see the benchmark code to find
out what you did measure :-)

regards,
Vlad



More information about the erlang-questions mailing list