Here’s a quick post on how to enable JIT (“just-in-time”) compiling in R. It’s very simple, and detailed in much greater depth in this excellent post by Tal Galili.

So first: why do we want to use a JIT compiler? Put simply, it will almost certainly speed up your code if you’re doing anything fairly strenuous. JIT compiling essentially compiles your R code into byte code just in time to use it. This will slow down the processing of the code for the first iteration (because there’s another step between telling R to execute the code and when R actually executes it), but speed it up for all following iterations. When you consider that functions you write yourself (as well as third party packages) can all be compiled, this will generally result in fairly significant performance increases. It’s also very simple. If you want to see the sort of speed increases you should expect, write a function whose speed you would like to measure:

require(compiler)
f <- function() {
  ...
}
cf <- cmpfun(f)
system.time(replicate(n,f()))
system.time(replicate(n,cf())

That is, I have some function that will be repeatedly executed n times. You’ll want to make n large enough that it takes some fairly significant chunk of time – like maybe 20 seconds or so. You will very likely see an improvement through compilation. The first replicate will be faster when uncompiled, but every subsequent iteration will tend to be faster through compilation. If you’re using a lot of external functions in this test function, don’t expect miraculous speed increases. This is something else that will need to be fiddled with. I believe that the compiler function is now a part of base R, but you may need to install it. I don’t remember, offhand. You can find examples detailed in the compiler documentation.

To enable JIT compilation, it suffices to stick a couple of variables in your R environment (it’s a file called .Renviron and it should be in your home directory – that is, the directory in which R starts up by default). If you don’t have this file, you should create it. Then add the following two lines:

R_COMPILE_PKGS=TRUE
R_ENABLE_JIT=3

The first line, logically enough, directs R to compile external packages. The second line directs R to enable to JIT compiler. The 3 indicates the level of JIT compiling. From the R documentation:

JIT is disabled if the argument is 0. If enable is 1 then closures are compiled before their first use. If enable is 2, then in addition closures are also compiled before they are duplicated (useful for some packages, like lattice, that store closures in lists). If enable is 3 then in addition all loops are compiled before they are executed.

Basically, 3 is the level that does full JIT compilation. It’s also possible to enable these options from within an active R session (or should be), but as I recall, this didn’t work as I expected, somehow. It may be that packages loaded prior to enabling will not be compiled (for instance, base R functions – you know, the ones you probably use the most). In any event, this was the solution I liked best.

You’ll need to restart R for these changes to take effect.