[Rcpp-commits] r2145 - pkg/Rcpp/inst/examples/ConvolveBenchmarks
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Sep 23 16:37:06 CEST 2010
Author: romain
Date: 2010-09-23 16:37:05 +0200 (Thu, 23 Sep 2010)
New Revision: 2145
Added:
pkg/Rcpp/inst/examples/ConvolveBenchmarks/loopmacro.h
Modified:
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_c.c
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_cpp.cpp
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve4_cpp.cpp
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve7_c.c
pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
Log:
looping internally
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_c.c
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_c.c 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_c.c 2010-09-23 14:37:05 UTC (rev 2145)
@@ -22,3 +22,7 @@
UNPROTECT(3);
return(ab);
}
+
+#include "loopmacro.h"
+LOOPMACRO_C(convolve2)
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_cpp.cpp 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve2_cpp.cpp 2010-09-23 14:37:05 UTC (rev 2145)
@@ -22,3 +22,7 @@
rs.add("ab", xab);
return rs.getReturnList();
}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve2cpp)
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp 2010-09-23 14:37:05 UTC (rev 2145)
@@ -18,3 +18,7 @@
return xab ;
}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve3cpp)
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve4_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve4_cpp.cpp 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve4_cpp.cpp 2010-09-23 14:37:05 UTC (rev 2145)
@@ -22,3 +22,8 @@
return xab ;
}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve4cpp)
+
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp 2010-09-23 14:37:05 UTC (rev 2145)
@@ -5,6 +5,7 @@
#include <Rcpp.h>
using namespace Rcpp ;
+
RcppExport SEXP convolve5cpp(SEXP a, SEXP b) {
NumericVector xa(a); int n_xa = xa.size() ;
NumericVector xb(b); int n_xb = xb.size() ;
@@ -15,3 +16,7 @@
}
return xab ;
}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve5cpp)
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve7_c.c
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve7_c.c 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve7_c.c 2010-09-23 14:37:05 UTC (rev 2145)
@@ -19,3 +19,7 @@
UNPROTECT(3);
return(ab);
}
+
+#include "loopmacro.h"
+LOOPMACRO_C(convolve7)
+
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r 2010-09-23 13:39:09 UTC (rev 2144)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r 2010-09-23 14:37:05 UTC (rev 2145)
@@ -15,39 +15,38 @@
## now run each one once for comparison of results,
## and define test functions
-v1 <- .Call("convolve2", a, b)
-R_API_optimised <- function(a,b) .Call("convolve2", a, b)
+R_API_optimised <- function(n,a,b) .Call("convolve2__loop", n, a, b)
+Rcpp_Classic <- function(n,a,b) .Call("convolve2cpp__loop", n, a, b)
+Rcpp_New_std <- function(n,a,b) .Call("convolve3cpp__loop", n, a, b)
+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)
-v2 <- .Call("convolve2cpp", a, b)[[1]]
-stopifnot(all.equal(v1, v2))
-Rcpp_Classic <- function(a,b) .Call("convolve2cpp", a, b)
+v1 <- R_API_optimised(1L, a, b )
+v2 <- Rcpp_Classic(1L,a,b)[[1]]
+v3 <- Rcpp_New_std(1L, a, b)
+v4 <- Rcpp_New_ptr(1L, a, b)
+v5 <- Rcpp_New_sugar(1L, a, b )
+v7 <- R_API_naive(1L, a, b)
-v3 <- .Call("convolve3cpp", a, b)
+stopifnot(all.equal(v1, v2))
stopifnot(all.equal(v1, v3))
-Rcpp_New_std <- function(a,b) .Call("convolve3cpp", a, b)
-
-v4 <- .Call("convolve4cpp", a, b)
stopifnot(all.equal(v1, v4))
-Rcpp_New_ptr <- function(a,b) .Call("convolve4cpp", a, b)
-
-v5 <- .Call( "convolve5cpp", a, b )
stopifnot(all.equal(v1, v5))
-Rcpp_New_sugar <- function(a,b) .Call("convolve5cpp", a, b)
+stopifnot(all.equal(v1, v7))
-v7 <- .Call("convolve7", a, b)
-R_API_naive <- function(a,b) .Call("convolve7", a, b)
-
## load benchmarkin helper function
suppressMessages(library(rbenchmark))
-bm <- benchmark(R_API_optimised(a,b),
- R_API_naive(a,b),
- Rcpp_Classic(a,b),
- Rcpp_New_std(a,b),
- Rcpp_New_ptr(a,b),
- Rcpp_New_sugar(a,b),
+REPS <- 10000L
+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"),
order="relative",
- replications=10000)
+ replications=1)
print(bm)
cat("All results are equal\n") # as we didn't get stopped
Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/loopmacro.h
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/loopmacro.h (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/loopmacro.h 2010-09-23 14:37:05 UTC (rev 2145)
@@ -0,0 +1,13 @@
+
+#define LOOPMACRO_C(name) \
+SEXP name##__loop(SEXP n_, SEXP a, SEXP b){ \
+ int n = INTEGER(n_)[0] ; \
+ SEXP res = R_NilValue ; \
+ for( int i=0; i<n; i++){ \
+ res = name( a, b ) ; \
+ } \
+ return res ; \
+}
+
+#define LOOPMACRO_CPP(name) RcppExport LOOPMACRO_C(name)
+
More information about the Rcpp-commits
mailing list