Interesting result of performance with string ++ vs prepend to list of string to append a list of string

Kaiduan Xie <>
Sat Aug 7 02:35:12 CEST 2010


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).

12> test:test_string_contact(1000000).
string++ time: 0.031/0.031 micro-seconds (runtime/wallclock)
ok
13> test:test_list_contact(1000000).
list contact time: 0.125/0.124 micro-seconds (runtime/wallclock)

Any idea why?

Thanks,

Kaiduan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.erl
Type: application/octet-stream
Size: 3119 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20100806/50217747/attachment.obj>


More information about the erlang-questions mailing list