r - Can I expect speed improvements from using Rcpp -


recently stumpled upon rcpp package , saw it's massive speed improvements on loops. programming simulation matrix algebra on 5d-arrays. example looks this:

library(rbenchmark) mat<- assign("mat",array(10,c(10,10,9,4,9))) mat2<-matrix(c(runif(16,0,1)),nrow=4,ncol=4) funcr<-function(x){   for(i in 1:dim(mat)[1]){     for(j in 1:dim(mat)[2]){       for(loc in 1:dim(mat)[3]){         for(yr in 1:dim(mat)[5]){           mat[i,j,loc,,yr]<<-floor(x%*%mat[i,j,loc,,yr])         }       }     }   } } benchmark(funcr(mat2))   test replications elapsed relative user.self sys.self user.child sys.child 1 funcr(mat2)          100  17.008      1     7.228    0.016          0         0 

now briefly reading introduction on how use rcpp package , before dive deeper wanted know whether in cases described above can gain something. asking because me non programmer mean time investment want make sure worth. rational behind complex arrays every step of script matrix multiplication on different dimension of array simulate development time various related things. open improvements , not sticking particular way of setting up. failed getting improvements using vectorization

you should similar performance rcpp using matrix multiply bit more directly.

use aperm bring 4th dimension front, flatten 2 dimensions. can 1 %*% , reverse process.

> mat3 <- aperm(mat, c(4,1,2,3,5)) > dim(mat3) [1]  4 10 10  9  9 > dim(mat3) <- c(4,prod(10,10,9,9)) > mat4 <- mat2 %*% mat3  > dim(mat4) [1]    4 8100 > dim(mat4) <- c(4,10,10,9,9) > mat5 <- aperm(mat4, c(2,3,4,1,5)) 

to double check:

# inner loop > mat2 %*% mat[1,1,1,,1]              [,1] [1,]  6.357168721 [2,] 18.288843057 [3,] 21.215756948 [4,] 10.310288982 > mat5[1,1,1,,1] [1]  6.357168721 18.288843057 21.215756948 10.310288982 

lgtm.

i've seen pattern quite bit in "scholarly" code, not in industry. trade off memory shortness of code, because multiply no longer in place.

if have control of other parts of code base, might consider changing memory layout of mat object , aperms won't necessary


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -