[Rcpp-devel] Initialization through constructor or through assignment?

Dirk Eddelbuettel edd at debian.org
Fri Oct 21 23:27:01 CEST 2011


Here is a slightly better setup with an 'empty' function which allows us to
get an idea of the cost of the additional statements: either the copy
constructor or the constructor.

R> suppressMessages(require(inline))
R> suppressMessages(require(rbenchmark))
R> suppressMessages(require(microbenchmark))
R> 
R> ctrFun <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
+    Rcpp::NumericMatrix x(xs);
+    return R_NilValue;
+ ')
R> 
R> cpyFun <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
+    Rcpp::NumericMatrix x = Rcpp::NumericMatrix(xs);
+    return R_NilValue;
+ ')
R> 
R> nilFun <- cxxfunction(signature(xs="numeric"), plugin="Rcpp", body='
+    return R_NilValue;
+ ')
R> 
R> M <- matrix(seq(1.0, 100.0^2, by=1.0), 100, 100)
R> benchmark(ctrFun(M), cpyFun(M), nilFun(M), order="relative", replications=1e6)
       test replications elapsed relative user.self sys.self user.child sys.child
3 nilFun(M)      1000000   4.873  1.00000      4.88     0.00          0         0
2 cpyFun(M)      1000000   5.065  1.03940      5.06     0.00          0         0
1 ctrFun(M)      1000000   5.228  1.07285      5.20     0.03          0         0
R> microbenchmark(ctrFun(M), cpyFun(M), nilFun(M), times=1e6)
Unit: nanoseconds
       expr  min   lq median   uq      max
1 cpyFun(M) 1130 1290   1366 1451 52959303
2 ctrFun(M) 1139 1299   1375 1459   995858
3 nilFun(M)  936 1069   1124 1205 53038920
R> 


Dirk


-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list