Hi,
I wonder about the time distribution obtained with microbenchmark and rcpp packages.
The R code is here:
R script
library(Rcpp)
library(microbenchmark)
library(ggplot2)
sourceCpp('test.cpp')
nrows <- 200
ncols <- 200
l <- nrows*ncols
#data <- runif(l)
r <- matrix(runif(l), nrow = nrows, ncol = ncols) # random numeric matrix
a <- matrix(numeric(l), nrow = nrows, ncol = ncols) # zero numeric matrix
b <- matrix(numeric(l), nrow = nrows, ncol = ncols) # zero numeric matrix
c <- matrix(as.numeric(1:l), nrow = nrows, ncol = ncols) # incremented numerix matrix (col major)
myrfunc <- function(a_in) # pas arg by value (sigh)
{
ncols=ncol(a)
nrows=nrow(a)
for (j in 1:ncols)
{
for (i in 1:nrows)
{
a_in[i,j] <- a_in[i,j]+i+100.0*j
}
}
return(a_in)
}
allm <- microbenchmark("r function" = {a<-myrfunc(a)},
"C++ matrix_update " = {matrix_update(b)},
"C++ matrix_update_bis " = {matrix_update_bis(b)})
autoplot(allm)
ggsave("test_perf.png")
test.cpp
#include <Rcpp.h>
using namespace Rcpp;
inline size_t getindex(size_t i,size_t j,size_t nr,size_t nc) {
return i+nr*j;
}
// [[Rcpp::export]]
void matrix_update(NumericMatrix a) {
//std::cout<< "nrows="<<a.nrow()<<std::endl;
//std::cout<< "ncols="<<a.ncol()<<std::endl;
const size_t nr=a.nrow();
const size_t nc=a.ncol();
const size_t l=nr*nc;
for (size_t j=0 ; j<nc ; j++){
for (size_t i=0 ; i<nr ; i++){
a[getindex(i,j,nr,nc)]+=double(i+1)+100.0*double(j+1);
}
}
}
// [[Rcpp::export]]
void matrix_update_bis(NumericMatrix a) {
//std::cout<< "nrows="<<a.nrow()<<std::endl;
//std::cout<< "ncols="<<a.ncol()<<std::endl;
const size_t nr=a.nrow();
const size_t nc=a.ncol();
for (size_t j=0 ; j<nc ; j++){
for (size_t i=0 ; i<nr ; i++){
a(i,j)+=double(i+1)+100.0*double(j+1);
}
}
}
which gives me the following output:
The minimal and mean C++ times are OK but the max value is very large. I am a complete R beginner and I do not understand what is happening.
Thank you for your help.
Laurent