[erlang-questions] Re: Interesting result of performance with string ++ vs prepend to list of string to append a list of string
Hynek Vychodil
hynek@REDACTED
Sat Aug 7 10:42:18 CEST 2010
As Robert pointed out, when you initialize strings in same function as
concatenate them you can be surprised by compiler optimizations. You
can check it easily using 'S' option to compiler
erlc +\'S\' test.erl
--------- 8< ------------
{function, string_contact, 0, 8}.
{label,7}.
{func_info,{atom,test},{atom,string_contact},0}.
{label,8}.
{move,{literal,"INVITE sip:1000@REDACTED SIP/2.0\r\nFrom:
sip:1000@REDACTED\r\nTo: sip:bob@REDACTED\r\nVia: SIP/2.0/UDP
serv
er.biloxi.com;branch=z9hgbK4bawer\r\nMax-Forwards: 70\r\nCall-ID:
a84b4c76e66710\r\nCSeq: 314519 INVITE\r\nContact:
<sip:alice@REDACTED
3>\r\nContent-TYpe: application/sdp\r\nContent-Length: 0\r\n"},
{x,0}}.
return.
--------- 8< ------------
So you don't concatenate those string at all. It is done in compile time.
On Sat, Aug 7, 2010 at 3:19 AM, Robert Virding <rvirding@REDACTED> 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.
>
> Robert
>
> On 7 August 2010 02:37, Kaiduan Xie <kaiduanx@REDACTED> wrote:
>> Sorry a typo, the result is that 1) is faster than 2).
>>
>> On Fri, Aug 6, 2010 at 8:35 PM, 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).
>>>
>>> 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
>>>
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>
>>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
--
--Hynek (Pichi) Vychodil
Analyze your data in minutes. Share your insights instantly. Thrill
your boss. Be a data hero!
Try GoodData now for free: www.gooddata.com
More information about the erlang-questions
mailing list