[Rcpp-devel] Loops, iterators and accumulate

Dirk Eddelbuettel edd at debian.org
Thu Nov 15 14:35:50 CET 2012


On 15 November 2012 at 07:24, Hadley Wickham wrote:
| 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:

Did you see the (fairly extensive) comparison in 
      examples/ConvolveBenchmarks/
which compares running across vectors in similar ways? There is more code in
the main file than currently tickled so you may want to revisit this in some
detail. Also compares to R's C API -- a fresh set of eyes (yours!!) would be
welcome. 
 
| > 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 ...

No, if I dare make a quick guess before heading to work it is because Romain
sneakily unrolls some loops for us in sugar --- fewer if() comparisons,
faster code.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list