[Rcpp-commits] r2154 - pkg/Rcpp/inst/examples/ConvolveBenchmarks

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 24 10:29:15 CEST 2010


Author: romain
Date: 2010-09-24 10:29:15 +0200 (Fri, 24 Sep 2010)
New Revision: 2154

Added:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve8_cpp.cpp
Removed:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2.R
Modified:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
Log:
trying to understand the overhead of NumericVector

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 08:04:13 UTC (rev 2153)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 08:29:15 UTC (rev 2154)
@@ -14,6 +14,7 @@
 R CMD SHLIB convolve3_cpp.cpp
 R CMD SHLIB convolve4_cpp.cpp
 R CMD SHLIB convolve5_cpp.cpp
+R CMD SHLIB convolve8_cpp.cpp
 
 # call R so that we get an interactive session
 Rscript exampleRCode.r

Deleted: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2.R
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2.R	2010-09-24 08:04:13 UTC (rev 2153)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2.R	2010-09-24 08:29:15 UTC (rev 2154)
@@ -1,6 +0,0 @@
-#!/usr/bin/r -t
-
-## Section 5.10.1 of 'Writing R Extensions' has a simple .Call example
-## for convolution which we are rewriting here
-
-

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve8_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve8_cpp.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve8_cpp.cpp	2010-09-24 08:29:15 UTC (rev 2154)
@@ -0,0 +1,43 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// this version is between the Rcpp_New_ptr and the Rcpp_New_std version
+//                          test elapsed  relative user.self sys.self
+// 5    Rcpp_New_ptr(REPS, a, b)   0.214  1.000000     0.213    0.001
+// 7  Rcpp_New_std_2(REPS, a, b)   0.223  1.042056     0.216    0.006
+// 4    Rcpp_New_std(REPS, a, b)   0.524  2.448598     0.523    0.001
+//
+// so there is some overhead due to creating Vec objects and indexing them
+// but much less than when we index the NumericVector
+
+#include <Rcpp.h>
+
+class Vec {
+public:
+    Vec( double* data_ ) : data(data_){}
+    inline double& operator[]( int i){ return data[i] ; }
+    
+private:
+    double* data ;
+} ;
+
+
+RcppExport SEXP convolve8cpp(SEXP a, SEXP b){
+    Rcpp::NumericVector xa(a);
+    Rcpp::NumericVector xb(b);
+    int n_xa = xa.size() ;
+    int n_xb = xb.size() ;
+    int nab = n_xa + n_xb - 1;
+    Rcpp::NumericVector xab(nab);
+    
+    Vec vab(xab.begin()), va(xa.begin()), vb(xb.begin()) ;
+    
+    for (int i = 0; i < n_xa; i++)
+        for (int j = 0; j < n_xb; j++) 
+            vab[i + j] += va[i] * vb[j];
+
+    return xab ;
+}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve8cpp)
+

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 08:04:13 UTC (rev 2153)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 08:29:15 UTC (rev 2154)
@@ -11,6 +11,7 @@
 dyn.load("convolve4_cpp.so")
 dyn.load("convolve5_cpp.so")
 dyn.load("convolve7_c.so")
+dyn.load("convolve8_cpp.so")
 
 ## now run each one once for comparison of results,
 ## and define test functions
@@ -21,7 +22,9 @@
 Rcpp_New_ptr <- function(n,a,b) .Call("convolve4cpp__loop", n, a, b)
 Rcpp_New_sugar <- function(n,a,b) .Call("convolve5cpp__loop", n, a, b)
 R_API_naive <- function(n,a,b) .Call("convolve7__loop", n, a, b)
+Rcpp_New_std_2 <- function(n,a,b) .Call("convolve8cpp__loop", n, a, b)
 
+
 v1 <- R_API_optimised(1L, a, b )
 v2 <- Rcpp_Classic(1L,a,b)[[1]]
 v3 <- Rcpp_New_std(1L, a, b)
@@ -37,14 +40,15 @@
 
 ## load benchmarkin helper function
 suppressMessages(library(rbenchmark))
-REPS <- 10000L
+REPS <- 20000L
 bm <- benchmark(R_API_optimised(REPS,a,b),
                 R_API_naive(REPS,a,b),
                 Rcpp_Classic(REPS,a,b),
                 Rcpp_New_std(REPS,a,b),
                 Rcpp_New_ptr(REPS,a,b),
                 Rcpp_New_sugar(REPS,a,b),
-                columns=c("test", "replications", "elapsed", "relative", "user.self", "sys.self"),
+                Rcpp_New_std_2(REPS,a,b),
+                columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
                 order="relative",
                 replications=1)
 print(bm)



More information about the Rcpp-commits mailing list