[Rcpp-commits] r2181 - in pkg/Rcpp/inst: examples/ConvolveBenchmarks include/Rcpp/sugar/nona include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 25 22:07:33 CEST 2010


Author: romain
Date: 2010-09-25 22:07:33 +0200 (Sat, 25 Sep 2010)
New Revision: 2181

Modified:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve12_cpp.cpp
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
   pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
Log:
bring convolve12 back into the results

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve12_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve12_cpp.cpp	2010-09-25 20:03:05 UTC (rev 2180)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve12_cpp.cpp	2010-09-25 20:07:33 UTC (rev 2181)
@@ -4,24 +4,21 @@
 
 #include <Rcpp.h>
 
-template <typename T>
-T convolve( const T& a, const T& b ){
-    int na = a.size() ; int nb = b.size() ;
-    T out(na + nb - 1);
-    typename T::iterator iter_a(a.begin()), iter_b(b.begin()), iter_ab( out.begin() ) ;
+RcppExport SEXP convolve12cpp(SEXP a, SEXP b){
+    Rcpp::NumericVector xa(a), xb(b);
+    int n_xa = xa.size(), n_xb = xb.size();
+    Rcpp::NumericVector xab(n_xa + n_xb - 1);
     
-    for (int i = 0; i < na; i++)
-        for (int j = 0; j < nb; j++) 
-            iter_ab[i + j] += iter_a[i] * iter_b[j];
+    typedef Rcpp::NumericVector::iterator vec_iterator ;
+    vec_iterator ia = xa.begin(), ib = xb.begin();
+    vec_iterator iab = xab.begin();
+    for (int i = 0; i < n_xa; i++)
+        for (int j = 0; j < n_xb; j++) 
+            iab[i + j] += ia[i] * ib[j];
 
-    return out ;
+    return xab;
 }
 
-
-RcppExport SEXP convolve12cpp(SEXP a, SEXP b){
-    return convolve( Rcpp::NumericVector(a), Rcpp::NumericVector(b) ) ;
-}
-
 #include "loopmacro.h"
 LOOPMACRO_CPP(convolve12cpp)
 

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-25 20:03:05 UTC (rev 2180)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-25 20:07:33 UTC (rev 2181)
@@ -50,7 +50,7 @@
 
 ## load benchmarkin helper function
 suppressMessages(library(rbenchmark))
-REPS <- 2000L
+REPS <- 5000L
 bm <- benchmark(R_API_optimised(REPS,a,b),
                 R_API_naive(REPS,a,b),
                 Rcpp_Classic(REPS,a,b),
@@ -62,11 +62,43 @@
 #                Rcpp_New_std_2(REPS,a,b),
 #                Rcpp_New_std_3(REPS,a,b),
 #                Rcpp_New_std_4(REPS,a,b),
-#                Rcpp_New_std_5(REPS,a,b),
+                Rcpp_New_std_5(REPS,a,b),
                 columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
                 order="relative",
                 replications=1)
 print(bm)
 
 cat("All results are equal\n") # as we didn't get stopped
-
+stop( "ok" )            
+sizes <- 1:10*100
+REPS <- 5000L
+timings <- lapply( sizes, function(size){
+    cat( "size = ", size, "..." )
+    a <- rnorm(size); b <- rnorm(size)
+    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),
+                Rcpp_New_sugar_nona(REPS,a,b),
+                columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
+                order="relative",
+                replications=1)
+     
+     cat( "  done\n" )
+     bm
+} )
+for( i in seq_along(sizes)){
+    timings[[i]]$size <- sizes[i]
+}
+timings <- do.call( rbind, timings )
+ 
+require( lattice )
+png( "elapsed.png", width = 800, height = 600 )
+xyplot( elapsed ~ size, groups = test, data = timings, auto.key = TRUE, type = "l", lwd = 2 )
+dev.off()
+png( "relative.png", width = 800, height = 600 )
+xyplot( relative ~ size, groups = test, data = timings, auto.key = TRUE, type = "l", lwd = 2 )
+dev.off()
+   

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	2010-09-25 20:03:05 UTC (rev 2180)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/nona/nona.h	2010-09-25 20:07:33 UTC (rev 2181)
@@ -77,6 +77,9 @@
 inline sugar::NonaPrimitive<double> nona( double x ){
     return sugar::NonaPrimitive<double>( x ) ; 
 }
+inline sugar::NonaPrimitive<int> nona( int x ){
+    return sugar::NonaPrimitive<int>( x ) ; 
+}
 
 
 }

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-09-25 20:03:05 UTC (rev 2180)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-09-25 20:07:33 UTC (rev 2181)
@@ -329,9 +329,6 @@
 	inline iterator begin() const{ return cache.get() ; }
 	inline iterator end() const{ return cache.get(size()) ; }
 	
-	// inline Proxy operator[]( int i ){ return cache.ref(i) ; }
-	// inline Proxy operator[]( int i ) const { return cache.ref(i) ; }
-	
 	inline Proxy operator[]( int i ){ return iter_first[i] ; }
 	inline Proxy operator[]( int i ) const { return iter_first[i] ; }
 	inline Proxy operator()( const size_t& i) throw(index_out_of_bounds){



More information about the Rcpp-commits mailing list