[erlang-questions] Re: Interesting result of performance with string ++ vs prepend to list of string to append a list of string
Sat Aug 7 06:19:27 CEST 2010
Thank you very much for the reply. I know the two operations are not
equivalent. For the second case, lists:flatten(lists:reverse(R2))
achieves the same result as 1), but lists:flatten(lists:reverse(R2))
even consumes more time than 1).
So ++ is the best choice if I want to concatenate strings.
On Fri, Aug 6, 2010 at 9:19 PM, Robert Virding <> wrote:
> Two things:
> - the two operations you are doing are not equivalent, if you check
> the resultant lists you will see they are different.
> - I would not initialise the strings in each test function as you have
> done. While I think the BEAM does not build strings each time as it
> did before, it still feels wrong to do it that way. :-)
> And maybe it is because ++ isn't as bad as people think, as long as
> you use it wisely. In your case their is not really much else you can
> do if you actually do want to concatenate the strings.
> On 7 August 2010 02:37, Kaiduan Xie <> wrote:
>> 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];
>>> 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)
>>> 13> test:test_list_contact(1000000).
>>> list contact time: 0.125/0.124 micro-seconds (runtime/wallclock)
>>> Any idea why?
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:
More information about the erlang-questions