[Rcpp-commits] r1654 - pkg/Rcpp/inst/examples/SugarPerformance
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 22 23:28:47 CEST 2010
Author: edd
Date: 2010-06-22 23:28:47 +0200 (Tue, 22 Jun 2010)
New Revision: 1654
Modified:
pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R
Log:
some changes -- and sugar looses the performance race :-/
Modified: pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R 2010-06-22 21:28:18 UTC (rev 1653)
+++ pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R 2010-06-22 21:28:47 UTC (rev 1654)
@@ -2,21 +2,19 @@
suppressMessages(library(inline))
suppressMessages(library(Rcpp))
-
# RcppExport SEXP vectorOps(SEXP runss, SEXP xs, SEXP ys) {
src <- '
- Rcpp::NumericVector x(xs);
- Rcpp::NumericVector y(ys);
- unsigned int runs = Rcpp::as<int>(runss);
+ NumericVector x(xs);
+ NumericVector y(ys);
+ unsigned int runs = as<int>(runss);
int n = x.size() ;
- Rcpp::NumericVector res(2);
- Timer timerOne, timerTwo;
+ Timer timer;
// approach one
- timerOne.Start();
+ timer.Start();
for (unsigned int i=0; i<runs; i++) {
- Rcpp::NumericVector res1( n ) ;
+ NumericVector res1( n ) ;
double x_ = 0.0 ;
double y_ = 0.0 ;
for( int i=0; i<n; i++){
@@ -29,94 +27,98 @@
}
}
}
- timerOne.Stop();
- //double t1 = timerOne.ElapsedTime();
- res[0] = timerOne.ElapsedTime();
- //timer.Reset();
+ timer.Stop();
+ double t1 = timer.ElapsedTime();
+ timer.Reset();
// approach two
- timerTwo.Start();
+ timer.Start();
for (unsigned int i=0; i<runs; i++) {
- Rcpp::NumericVector res2 = ifelse( x < y, x*x, -(y*y) ) ;
+ NumericVector res2 = ifelse( x < y, x*x, -(y*y) ) ;
}
timer.Stop();
- //double t2 = timerTwo.ElapsedTime();
- res[1] = timer.ElapsedTime();
+ double t2 = timer.ElapsedTime();
- return Rcpp::wrap(res);
- // FAILS: return Rcpp::List(Rcpp::Named("one")=Rcpp::wrap(t1),
- // Rcpp::Named("two")=Rcpp::wrap(t2));
+ // FIXME return List(Named("standard") = t1,
+ // Named("sugar") = t2);
+ NumericVector v(2); v[0] = t1, v[1] = t2;
+ return v;
'
+## srcOne <- '
+## Rcpp::NumericVector x(xs);
+## Rcpp::NumericVector y(ys);
+## unsigned int runs = Rcpp::as<int>(runss);
+## int n = x.size() ;
-srcOne <- '
- Rcpp::NumericVector x(xs);
- Rcpp::NumericVector y(ys);
- unsigned int runs = Rcpp::as<int>(runss);
- int n = x.size() ;
+## Timer timer;
- Timer timer;
+## timer.Start();
+## for (unsigned int i=0; i<runs; i++) {
+## Rcpp::NumericVector res1( n ) ;
+## double x_ = 0.0 ;
+## double y_ = 0.0 ;
+## for( int i=0; i<n; i++){
+## x_ = x[i] ;
+## y_ = y[i] ;
+## if( x_ < y_ ){
+## res1[i] = x_ * x_ ;
+## } else {
+## res1[i] = -( y_ * y_) ;
+## }
+## }
+## }
+## timer.Stop();
+## return Rcpp::wrap( timer.ElapsedTime() );
+## '
- timer.Start();
- for (unsigned int i=0; i<runs; i++) {
- Rcpp::NumericVector res1( n ) ;
- double x_ = 0.0 ;
- double y_ = 0.0 ;
- for( int i=0; i<n; i++){
- x_ = x[i] ;
- y_ = y[i] ;
- if( x_ < y_ ){
- res1[i] = x_ * x_ ;
- } else {
- res1[i] = -( y_ * y_) ;
- }
- }
- }
- timer.Stop();
- return Rcpp::wrap( timer.ElapsedTime() );
-'
+## srcTwo <- '
+## Rcpp::NumericVector x(xs);
+## Rcpp::NumericVector y(ys);
+## unsigned int runs = Rcpp::as<int>(runss);
-srcTwo <- '
- Rcpp::NumericVector x(xs);
- Rcpp::NumericVector y(ys);
- unsigned int runs = Rcpp::as<int>(runss);
+## Timer timer;
- Timer timer;
+## timer.Start();
+## for (unsigned int i=0; i<runs; i++) {
+## Rcpp::NumericVector res = ifelse( x < y, x*x, -(y*y) ) ;
+## }
+## timer.Stop();
+## return Rcpp::wrap( timer.ElapsedTime() );
+## '
- timer.Start();
- for (unsigned int i=0; i<runs; i++) {
- Rcpp::NumericVector res = ifelse( x < y, x*x, -(y*y) ) ;
- }
- timer.Stop();
- return Rcpp::wrap( timer.ElapsedTime() );
-'
+settings <- getPlugin("Rcpp")
+settings$env$PKG_CXXFLAGS <- paste("-I", getwd(), " -O0", sep="")
+## funOne <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
+## srcOne,
+## includes='#include "Timer.h"',
+## plugin="Rcpp",
+## settings=settings)
+## funTwo <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
+## srcTwo,
+## includes='#include "Timer.h"',
+## plugin="Rcpp",
+## settings=settings)
-settings <- getPlugin("Rcpp")
-settings$env$PKG_CXXFLAGS <- paste("-I", getwd(), " -O0", sep="")
+x <- runif(1e5)
+y <- runif(1e5)
+runs <- 500
-funOne <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
- srcOne,
- includes='#include "Timer.h"',
- plugin="Rcpp",
- settings=settings)
-funTwo <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
- srcTwo,
- includes='#include "Timer.h"',
- plugin="Rcpp",
- settings=settings)
+#resOne <- funOne(runs, x, y)
+#resTwo <- funTwo(runs, x, y)
+#cat("Timings: Explicit ", resOne, "vs Sugar:", resTwo, ifelse(resOne > resTwo, "win", "loss"), "\n")
+#Does order matter?
+#resTwo <- funTwo(runs, x, y)
+#resOne <- funOne(runs, x, y)
+#cat("Timings: Explicit ", resOne, "vs Sugar:", resTwo, ifelse(resOne > resTwo, "win", "loss"), "\n")
-x <- runif(1e6)
-y <- runif(1e6)
-runs <- 10
-resOne <- funOne(runs, x, y)
-resTwo <- funTwo(runs, x, y)
-cat("Timings: ", resOne, "vs", resTwo, "\n")
-
-# Does order matter?
-resTwo <- funTwo(runs, x, y)
-resOne <- funOne(runs, x, y)
-cat("Timings: ", resOne, "vs", resTwo, "\n")
+fun <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
+ src,
+ includes='#include "Timer.h"',
+ plugin="Rcpp",
+ settings=settings)
+print(fun(runs, x, y))
More information about the Rcpp-commits
mailing list