A blog about statistics, probability, mathematics and data visualization.

Friday, April 27, 2012

Speeding up R computations Pt II: compiling

A year ago I wrote a post on speeding up R computations. Some of the tips that I mentioned then have since been made redundant by a single package: compiler. Forget about worrying about curly brackets and whether to write 3*3 or 3^2 - compiler solves those problems for you.

compiler provides a byte code compiler for R, which offers the possibility of compiling your functions before executing them. Among other things, this speeds up those embarrassingly slow for loops that you've been using:

Functions written in C (imported using Rcpp) are still much faster than the compiled byte code, but for those of us who

don't know C,

know C but prefer to write code in R,

know C but don't want to rewrite functions that we've already written in R,

compiler offers a great way of speeding up computations. It's included in the recommended R packages since R 2.13 (meaning that it comes with your basic R installation) and since R 2.14 most standard functions are already compiled. If you still are running an older version of R it's definitely time to update.