Pages

Saturday, May 05, 2012

Compiling R code, and speed up your computation

I just ran into this interesting post on the R-bloggers Planet. The described R functionality allows you to compile R code (to byte code) so that it will no longer be interpreted but actually run. That is a performance boost. I guess in due time we will see R use JIT technologies, so that the difference will disappear, but for now, a good thing to know about. Here are the numbers from that post:

> system.time( myFunction() )
   user  system elapsed 
 10.002   0.014  10.021 
> system.time( myCompiledFunction() )
   user  system elapsed 
  0.692   0.008   0.700 

Compiling a function seems pretty easy, and I will give that a try soon, but not today:

> library(compiler)
> myCompiledFunction <- cmpfun(myFunction)

Thanx to Måns at Uppsala University :)

3 comments:

  1. p.s, you might find value in reading this post:
    http://www.r-statistics.com/2012/04/speed-up-your-r-code-using-a-just-in-time-jit-compiler/

    ReplyDelete
    Replies
    1. Ah, thanx for the pointer! If I understand things correctly, you only need to add the following lines, so that you do not have to compile all functions yourself:

      require(compiler)
      enableJIT(3)

      Delete
    2. Indeed.
      Although notice that this might be sometimes buggy, and the safer path will be to use:

      require(compiler)
      enableJIT(1)

      Cheers,
      Tal

      Delete