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^2compiler 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:


> myFunction<-function() { for(i in 1:10000000) { 1*(1+1) } }
> library(compiler)
> myCompiledFunction <- cmpfun(myFunction) # Compiled function

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

That's 14 times faster!


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.

5 comments:

  1. Hi Mans,
    You might be interested in a post I recently wrote about using the JIT in the compiler package:
    http://www.r-statistics.com/2012/04/speed-up-your-r-code-using-a-just-in-time-jit-compiler/

    ReplyDelete
  2. Thanks for the link Tal, that was a very interesting read! :)

    ReplyDelete
  3. Thanx for pointing me to this corner of R!

    Greatings from a former UU post-doc (BioMedicum)!

    ReplyDelete
  4. Manohar Reddy

    dataentryoutsource2india@gmail.com

    http://dataentryoutsource2india.com/


    Dataentryoutsource2india.com specializes in providing excellent services on Data Entry, Data Processing, Document Scanning Services, Data Conversion service, E-Publishing service, XML Tagging service, Online Data Extraction, Image Editing/Graphic Support, Order Processing, Catalog Processing and lots more with high quality and very affordable price.














    ReplyDelete
  5. Manohar Reddy

    http://dataentryoutsource2india.com


    dataentryoutsource2india@gmail.com

    Dataentryoutsource2india.com specializes in providing excellent services on Data Entry, Data Processing, Document Scanning Services, Data Conversion service, E-Publishing service, XML Tagging service, Online Data Extraction, Image Editing/Graphic Support, Order Processing, Catalog Processing and lots more with high quality and very affordable price.


    ReplyDelete