I would recommend profiling the function using Rprof
or the profr
package. This will show you where your bottleneck is, and you then you can think about ways to either optimize the function or change the way that you're using it.
Your paste
example would be much faster in part because it's vectorized. For a more fair comparison, you can see the difference there by looping over paste
as you are currently doing with newXMLNode
and see the difference in timing.
Edit:
Here is the output from profiling your loop with profr
.
library(profr)
xml.prof <- profr(for(i in 1:N)
newXMLNode("Parameter", parent=seq, attrs=c(id=pars[i])))
plot(xml.prof)
There is nothing especially obvious in here about places that you can improve this. I see that it spends a reasonable amount of time in the %in%
function, so improving that would reduce the overall time somewhat (although you still need to iterate over this repeatedly, so it won't make a huge difference). The best solution would be to rewrite newXMLNode
as a vectorized function so you can skip the for
loop entirely.