Rcpp Version 0.9.10
exampleRCode.r
Go to the documentation of this file.
00001 #!/usr/bin/r
00002 
00003 suppressMessages(require(Rcpp))
00004 set.seed(42)
00005 n <- 200
00006 a <- rnorm(n)
00007 b <- rnorm(n)
00008 
00009 ## load shared libraries with wrapper code
00010 dyn.load("convolve2_c.so")
00011 dyn.load("convolve3_cpp.so")
00012 dyn.load("convolve4_cpp.so")
00013 dyn.load("convolve5_cpp.so")
00014 dyn.load("convolve7_c.so")
00015 
00016 dyn.load("convolve8_cpp.so")
00017 dyn.load("convolve9_cpp.so")
00018 dyn.load("convolve10_cpp.so")
00019 dyn.load("convolve11_cpp.so")
00020 dyn.load("convolve12_cpp.so" )
00021 dyn.load("convolve14_cpp.so" )
00022 
00023 ## now run each one once for comparison of results,
00024 ## and define test functions
00025 
00026 R_API_optimised <- function(n,a,b) .Call("convolve2__loop", n, a, b)
00027 Rcpp_New_std <- function(n,a,b) .Call("convolve3cpp__loop", n, a, b)
00028 #Rcpp_New_std_inside <- function(n,a,b) .Call("convolve3cpp__loop", n, a, b, PACKAGE = "Rcpp" )
00029 Rcpp_New_ptr <- function(n,a,b) .Call("convolve4cpp__loop", n, a, b)
00030 Rcpp_New_sugar <- function(n,a,b) .Call("convolve5cpp__loop", n, a, b)
00031 Rcpp_New_sugar_noNA <- function(n,a,b) .Call("convolve11cpp__loop", n, a, b)
00032 R_API_naive <- function(n,a,b) .Call("convolve7__loop", n, a, b)
00033 Rcpp_New_std_2 <- function(n,a,b) .Call("convolve8cpp__loop", n, a, b)
00034 #Rcpp_New_std_3 <- function(n,a,b) .Call("convolve9cpp__loop", n, a, b)
00035 #Rcpp_New_std_4 <- function(n,a,b) .Call("convolve10cpp__loop", n, a, b)
00036 Rcpp_New_std_it <- function(n,a,b) .Call("convolve12cpp__loop", n, a, b )
00037 Rcpp_New_std_Fast <- function(n,a,b) .Call("convolve14cpp__loop", n, a, b )
00038 
00039 
00040 v1 <- R_API_optimised(1L, a, b )
00041 v3 <- Rcpp_New_std(1L, a, b)
00042 v4 <- Rcpp_New_ptr(1L, a, b)
00043 v5 <- Rcpp_New_sugar(1L, a, b )
00044 v7 <- R_API_naive(1L, a, b)
00045 v11 <- Rcpp_New_sugar_noNA(1L, a, b)
00046 
00047 stopifnot(all.equal(v1, v3))
00048 stopifnot(all.equal(v1, v4))
00049 stopifnot(all.equal(v1, v5))
00050 stopifnot(all.equal(v1, v7))
00051 stopifnot(all.equal(v1, v11))
00052 
00053 ## load benchmarkin helper function
00054 suppressMessages(library(rbenchmark))
00055 REPS <- 5000L
00056 bm <- benchmark(R_API_optimised(REPS,a,b),
00057                 R_API_naive(REPS,a,b),
00058                 Rcpp_New_std(REPS,a,b),
00059 #                Rcpp_New_std_inside(REPS,a,b),
00060                 Rcpp_New_ptr(REPS,a,b),
00061                 Rcpp_New_sugar(REPS,a,b),
00062                 Rcpp_New_sugar_noNA(REPS,a,b),
00063                 Rcpp_New_std_2(REPS,a,b),
00064 #                Rcpp_New_std_3(REPS,a,b),
00065 #                Rcpp_New_std_4(REPS,a,b),
00066                 Rcpp_New_std_it(REPS,a,b),
00067                 Rcpp_New_std_Fast(REPS,a,b),
00068                 columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
00069                 order="relative",
00070                 replications=1)
00071 print(bm)
00072 
00073 cat("All results are equal\n") # as we didn't get stopped
00074 q("no")
00075 
00076 
00077 sizes <- 1:10*100
00078 REPS <- 5000L
00079 timings <- lapply( sizes, function(size){
00080     cat( "size = ", size, "..." )
00081     a <- rnorm(size); b <- rnorm(size)
00082     bm <- benchmark(R_API_optimised(REPS,a,b),
00083                 R_API_naive(REPS,a,b),
00084                 Rcpp_New_std(REPS,a,b),
00085                 Rcpp_New_ptr(REPS,a,b),
00086                 Rcpp_New_sugar(REPS,a,b),
00087                 Rcpp_New_sugar_noNA(REPS,a,b),
00088                 columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
00089                 order="relative",
00090                 replications=1)
00091 
00092      cat( "  done\n" )
00093      bm
00094 } )
00095 for( i in seq_along(sizes)){
00096     timings[[i]]$size <- sizes[i]
00097 }
00098 timings <- do.call( rbind, timings )
00099 
00100 require( lattice )
00101 png( "elapsed.png", width = 800, height = 600 )
00102 xyplot( elapsed ~ size, groups = test, data = timings, auto.key = TRUE, type = "l", lwd = 2 )
00103 dev.off()
00104 png( "relative.png", width = 800, height = 600 )
00105 xyplot( relative ~ size, groups = test, data = timings, auto.key = TRUE, type = "l", lwd = 2 )
00106 dev.off()
00107 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines