Patty
Patty

Reputation: 167

Could someone explain what "compiling" in R is, and why it would speed up this code?

I am working on some R code written by a previous student. The code is extremely computationally intensive, and for that reason he appears to have gone to great lengths to minimise the time it took in anyway possible.

One example is the following section:

# Now lets compile these functions, for a modest speed boost.
Sa <- cmpfun(Sa)
Sm <- cmpfun(Sm)
sa <- cmpfun(sa)
sm <- cmpfun(sm)
h <- cmpfun(h)
li <- cmpfun(lli)
ll <- cmpfun(ll)

He appears to have used the compiler package to do this.

I have never heard of compiling in R, and I am interested in what it does and why it would help speed up the code. I am having trouble finding material that would explain it for a novice like me.

Upvotes: 3

Views: 4765

Answers (1)

csgillespie
csgillespie

Reputation: 60492

The compiler package has been part of R since version 2.130. Compiling R functions, results in a byte code version that may run faster. There are a number ways of compiling. All base R functions are compiled by default.

Compiling individual functions via cmpfun. Alternatively, you can call enableJIT(3) once and the R code is automatically compiled.

I've found compiling R code gives a modest, cost free speed boost - see Efficient R programming for a timed example.

It appears that byte compiling will be turned on by default in R 3.4.X

Upvotes: 4

Related Questions