[erlang-questions] Interesting result of performance with string ++ vs prepend to list of string to append a list of string

Björn Gustavsson bgustavsson@REDACTED
Sun Aug 8 09:33:35 CEST 2010


On Sat, Aug 7, 2010 at 2:35 AM, Kaiduan Xie <kaiduanx@REDACTED> wrote:
> Hi, all,
>
> For a common operation like appending a list of string to get a new
> string, I tried two ways, simple ++, add the string to the head of a
> list of string and then reverse as below,
>
> L1 = "Welcome to erlang land\r\n",
> L2 = "Welcome to Canada\r\n",
>
> 1) L = L1 ++ L2.
>
> 2) R1 = [L1];
>    R2 = [L2  | R1];
>    lists:reverse(R2)
>
> I expect 2) would be faster than 1) because everyone is talking ++ is
> slow. However, the test result shows 2) is even faster than 1).

See Myth 2.4 in the Efficiency Guide:

http://www.erlang.org/doc/efficiency_guide/myths.html

++ is only bad if you use it improperly in a loop, such
as in the naive_reverse/1 example.

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list