[R] Performance of concatenating strings

hadley wickham h.wickham at gmail.com
Wed Oct 31 14:59:14 CET 2007

On 10/31/07, Tamara Steijger <smara1 at gmx.de> wrote:
> Hi,
> I would like to ask how the paste(S1, S2, sep="") function internally
> works. Are the two stings copied to a new String?

I'm not 100% sure, but I'd suspect so, as this is the default
behaviour in pretty much every programming language.

> I have a program where successively strings are build up. First the
> program calls an external function and depending on the result it
> builds up strings to visualize the result. The external function is
> really fast, also for huge input data. But the building of the
> strings takes much to long for huge input sizes. So I'm wondering if
> the concatenating could be the problem, like using String in Java
> instead of StringBuffer. Is there something like StringBuffers in R
> also?

If you know how many components there will be in the string, it's
probably best to create a character vector (str <- vector("character",
100)) filling it up as you go, and then pasting it together at the
very end.  If you don't know how many components there will be, you
will need to do something a bit more sophisticated.

You might also trying profiling your function to make sure the
slowness really is being caused by what you think it is.



More information about the R-help mailing list