Apparently it is easy to get a 3x speed increase by simply compiling a function.
Doing a lot of the MCMC stuff, I am particularly excited about speed in R. I just compiled a 2000-line file with code from my latest project, but none of the functions would run faster. Apparently I need to break down things a little more and use more subfunctions.
Well, so I tried a much easier example. I took runiregGibbs from the well known bayesm packages (which is a function completely written in R) and compiled it. There's a visible change, but it's quite small:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(compiler) | |
library(bayesm) | |
#compile runiregGibbs function | |
runiregGibbsc=cmpfun(runiregGibbs) | |
set.seed(66); n=1000; R=10000 | |
X=cbind(rep(1,n),runif(n)); beta=c(1,2); sigsq=.25 | |
y=X%*%beta+rnorm(n,sd=sqrt(sigsq)) | |
Data1=list(y=y,X=X); Mcmc1=list(R=R) | |
#run code | |
t.original = system.time(runiregGibbs(Data=Data1,Mcmc=Mcmc1)) | |
t.compiled = system.time(runiregGibbsc(Data=Data1,Mcmc=Mcmc1)) | |
#result | |
> t.original | |
User System verstrichen | |
2.20 0.11 2.30 | |
> t.compiled | |
User System verstrichen | |
1.90 0.20 2.11 |