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

Kaiduan Xie <>
Sat Aug 7 02:37:30 CEST 2010


Sorry a typo, the result is that 1) is faster than 2).

On Fri, Aug 6, 2010 at 8:35 PM, Kaiduan Xie <> 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).
>
> 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
>


More information about the erlang-questions mailing list