hadley/adv-r — pull request #462 by eriqande (2014-04-18 17:54:01+00:00)
Title: Update Rcpp.rmd

Hadley. You deserve to be knighted for all that you have done for R. Thanks so much.

I just noticed that if you pull the division by n outside of the for loop, you get about an 8-fold
improvement in speed over the vectorized R operation (and about 6x faster than meanC), as division is much costlier than addition or multiplication (if I remember correctly).

I did that and made a meanC2 function and got this:

```
Unit: microseconds
expr min lq median uq max neval
mean(x) 862.276 862.773 862.9605 863.3390 1071.375 100
meanC(x) 775.445 775.896 776.5775 777.1715 954.236 100
meanC2(x) 119.946 120.176 120.4670 121.1135 245.384 100
```

Your demonstration of how to micro benchmark c code here is fantastic. It really has helped me get
my head around when I should (and shouldn't) bother implementing something in compiled code.

I can't stress enough how helpful this book is. Thanks again.

eric