lists:append performance

Valentin Micic v@REDACTED
Fri Jul 24 14:20:32 CEST 2009

Hi all,


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've never
tested this assertion for a relatively small lists, thus, I've performed
some tests using a list of 240 elements, having each element as a string
like:  "The quick brown fox jumps over a lazy dog". I've written two
different functions:


1) insert (Count) that would always execute an insert like this:

2) append( Count ), which would always execute a call to lists:append(
OldList, ["1"] ).


I cannot say that I haven't been surprised by a result:


50> timer:tc( benchmarks, insert, [1000000000] ).



)51> timer:tc( benchmarks, append, [1000000000] ).



It seems that it doesn't make much of a difference if one uses one or
another. Would this hold for a larger lists as well, and if not - how large
a list should be in order to see a notable deterioration in lists:append/2


Thanks in advance.



More information about the erlang-questions mailing list