Hi Hadley,<div><br></div><div>To be fair with the R language, I would have compare with the use of real primitives :</div><div><div><br></div><div>vaccBubu <- function(age, female, ily) {</div><div>    gender <- female * 1.25</div>
<div>    gender[!female] <- 0.75</div><div>    p <- (0.25 + 0.3 * 1 / (1 - exp(0.04 * age)) + 0.1 * ily) * gender</div><div>    p[p < 0] <- 0</div><div>    p[p > 1] <- 1</div><div>    p</div><div>}</div>
</div><div><br></div><div>You can see that it's still not as good as compiled versions but the gap is not that big!</div><div><br></div><div><br></div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Mon, Nov 19, 2012 at 4:31 PM, Hadley Wickham <span dir="ltr"><<a href="mailto:h.wickham@gmail.com" target="_blank">h.wickham@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Hi all,<br>
<br>
Inspired by "Rcpp is smoking fast for agent-based models in data<br>
frames" (<a href="http://www.babelgraph.org/wp/?p=358" target="_blank">http://www.babelgraph.org/wp/?p=358</a>), I've been doing some<br>
exploration of vectorisation in R vs C++ at<br>
<a href="https://gist.github.com/4111256" target="_blank">https://gist.github.com/4111256</a><br>
<br>
I have five versions of the basic vaccinate function:<br>
<br>
* vacc1: vectorisation in R with a for loop<br>
* vacc2: used vectorised R primitives<br>
* vacc3: vectorised with loop in C++<br>
* vacc4: vectorised with Rcpp sugar<br>
* vacc5: vectorised with Rcpp sugar, explicitly labelled as containing<br>
no missing values<br>
<br>
And the timings I get are as follows:<br>
<br>
Unit: microseconds<br>
                    expr    min     lq median     uq     max neval<br>
 vacc1(age, female, ily) 6816.8 7139.4 7285.7 7823.9 10055.5   100<br>
 vacc2(age, female, ily)  194.5  202.6  212.6  227.9   260.4   100<br>
 vacc3(age, female, ily)   21.8   22.4   23.4   24.9    35.5   100<br>
 vacc4(age, female, ily)   36.2   38.7   41.3   44.5    55.6   100<br>
 vacc5(age, female, ily)   29.3   31.3   34.0   36.4    52.1   100<br>
<br>
Unsurprisingly the R loop (vacc1) is very slow, and proper<br>
vectorisation speeds it up immensely.  Interestingly, however, the C++<br>
loop still does considerably better (about 10x faster) - I'm not sure<br>
exactly why this is the case, but I suspect it may be because it<br>
avoids the many intermediate vectors that R requires.  The sugar<br>
version is about half as fast, but this gets quite a bit faster with<br>
explicit no missing flags.<br>
<br>
I'd love any feedback on my code (<a href="https://gist.github.com/4111256" target="_blank">https://gist.github.com/4111256</a>) -<br>
please let me know if I've missed anything obvious.<br>
<span class=""><font color="#888888"><br>
Hadley<br>
<br>
--<br>
RStudio / Rice University<br>
<a href="http://had.co.nz/" target="_blank">http://had.co.nz/</a><br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</font></span></blockquote></div><br></div>