Hi all, I'm trying to get a better handle on writing efficient numerical code with C++. I've attached five implementations of a simple sum function (mostly ignoring NAs) and timed them below:
> source("sum.r") Unit: microseconds expr min lq median uq max neval sum(x) 11.67 11.72 11.76 11.82 20.6 100 sum1(x) 9.58 9.68 9.75 9.82 19.7 100 sum2(x) 37.44 37.55 37.59 37.63 48.1 100 sum2a(x) 9.59 9.66 9.73 9.81 14.0 100 sum3(x) 31.84 31.92 31.98 32.03 35.6 100 sum4(x) 9.60 9.69 9.77 9.86 30.5 100 * sum: R's built in sum * sum1: loop * sum2: iterator * sum2a: iterator, but only compute x.end() once * sum3: use accumulate * sum4: use sugar sum My questions: * the R cpp introduction mentions that iterators are usually faster than using [ directly, but I don't see that here (or in a number of other places I've tried it). Why? * The introduction vignette also uses iterators in a (as far as I can tell) non-standard way - is that a mistake or some C++ magic I don't understand? (I've used them in standard style in the attached code) * why is accumulate so slow? * I'm slightly surprised that sugar::sum is about 20% faster than R's built-in sum. I would have expected base sum to be fairly well optimised and close to the metal. I guess this may be because R's sum deals with a few more cases of mixed arg types in ... Hadley -- RStudio / Rice University http://had.co.nz/
sum.r
Description: Binary data
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel