[erlang-questions] Which is best? string:concat or ++?
Joe Armstrong
erlang@REDACTED
Tue May 8 10:40:48 CEST 2012
Here a dumb answer.
"You might not need to concatenate them" - if all you're going to
do is output
the result later then you don't need to concatenate them. For example
X = A ++ B,
file:write_file("foo", X)
and
X = [A,B], %% <--- this thing is
called an iolist
file:write_file("foo", X)
will result in identical content in the file "foo". In this case
(where you're just going
send the concatenated data to an I/O stream) you don't need to bother
concatenating the data.
If you are building complex iolists (which is very common) and you need
to flatten it, then a good strategy is to build a single iolist (X)
and then call
erlang:iolist_to_binary(X).
Using '++' when you don't need to at all is a common programming error :-)
In the cases where you do need it probably both the A and B in A++B should be
small. If A is a long string then you should look for a different algorithm.
Cheers
/Joe
On Mon, May 7, 2012 at 3:31 PM, Paul Barry <paul.james.barry@REDACTED> wrote:
> Hi folks.
>
> This might be a case of a dumb question, but here goes anyway. :-)
>
> I have two strings, A and B. Is it better to use:
>
> string:concat(A, B)
>
> or
>
> A ++ B
>
> when combining them to create a new string? I'm writing some code
> that generates a chunk of HTML. I know that using ++ on big lists is
> regarded as a "no-no", but is it acceptable here?
>
> Thanks.
>
> Paul.
>
> --
> Paul Barry, w: http://paulbarry.itcarlow.ie - e: paul.barry@REDACTED
> Lecturer, Computer Networking: Institute of Technology, Carlow, Ireland.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list