[Rcpp-commits] r1665 - pkg/Rcpp/inst/examples/SugarPerformance

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 23 13:59:51 CEST 2010


Author: romain
Date: 2010-06-23 13:59:51 +0200 (Wed, 23 Jun 2010)
New Revision: 1665

Modified:
   pkg/Rcpp/inst/examples/SugarPerformance/Timer.h
   pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R
Log:
hand written code also using NA detection

Modified: pkg/Rcpp/inst/examples/SugarPerformance/Timer.h
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/Timer.h	2010-06-23 11:24:14 UTC (rev 1664)
+++ pkg/Rcpp/inst/examples/SugarPerformance/Timer.h	2010-06-23 11:59:51 UTC (rev 1665)
@@ -28,14 +28,10 @@
 #ifndef TIMER_H
 #define TIMER_H
 
-#include <sys/timex.h>			// Probably implies Linux-only ...
-
-int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
-
 class Timer {
 public:
-	Timer() {  Reset(); }
-	void Start()  { start_t = getFractionalSeconds(); }
+	Timer() : sys_time("Sys.time") { Reset(); }
+	void Start()  { start_t = getFractionalSeconds() ; }
 	void Stop() { 
 		end_t = getFractionalSeconds(); 
 		elapsed = end_t - start_t;			// Calculate elapsed time in seconds
@@ -47,21 +43,11 @@
 
 
 private:
+	Function sys_time ;
 	double start_t, end_t, elapsed, cumul;
 
 	double getFractionalSeconds(void) {
-        #if !defined(__WIN32__)
-		struct timeval tv; 											// see gettimeofday(2)
-		gettimeofday(&tv, NULL);
-		double t = (double) tv.tv_sec + (double) 1e-6 * tv.tv_usec; // seconds.microseconds since epoch 
-        #endif
-        #if defined(__WIN32__)
-		time_t temp = time(NULL);
-		SYSTEMTIME st; 
-		GetSystemTime(&st);
-		double t = temp + 1e-3*st.wMilliseconds;    				// milliseconds in windows
-        #endif
-		return(t);
+        return as<double>( sys_time() ) ;
 	}
 };
 

Modified: pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R	2010-06-23 11:24:14 UTC (rev 1664)
+++ pkg/Rcpp/inst/examples/SugarPerformance/vectorOps.R	2010-06-23 11:59:51 UTC (rev 1665)
@@ -2,7 +2,6 @@
 suppressMessages(library(inline))
 suppressMessages(library(Rcpp))
 
-# RcppExport SEXP vectorOps(SEXP runss, SEXP xs, SEXP ys) {
 src <- '
     NumericVector x(xs);
     NumericVector y(ys);
@@ -16,16 +15,18 @@
     Function sys_time( "Sys.time") ;
     double start = as<double>( sys_time( ) );
     for (unsigned int i=0; i<runs; i++) {
-        NumericVector res1( n ) ;
+   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_ ;
+            if( R_IsNA(x_) || R_IsNA(y_) ){
+                res1[i] = NA_REAL;
+            } else if( x_ < y_ ){
+                res1[i] = (x_ * x_) ;
             } else {
-                res1[i] = -( y_ * y_)  ;
+                res1[i] = - (y_ * y_) ;
             }
         }
     }
@@ -63,82 +64,16 @@
     	_["R"]     = t3
     	) ;
 '
-
-## srcOne <- '
-##     Rcpp::NumericVector x(xs);
-##     Rcpp::NumericVector y(ys);
-##     unsigned int runs = Rcpp::as<int>(runss);
-##     int n = x.size() ;
-
-##     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() );
-## '
-
-## srcTwo <- '
-##     Rcpp::NumericVector x(xs);
-##     Rcpp::NumericVector y(ys);
-##     unsigned int runs = Rcpp::as<int>(runss);
-
-##     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() );
-## '
-
 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)
-
-
 x <- runif(1e5)
 y <- runif(1e5)
 runs <- 500
 
-#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")
-
-
 fun <- cxxfunction(signature(runss="numeric", xs="numeric", ys="numeric"),
                    src,
-                   # includes='#include "Timer.h"',
-                   # includes = paste( readLines( "Timer.h" ), collapse = "\n" ),
+                   includes='#include "Timer.h"',
                    plugin="Rcpp",
                    settings=settings)
 print(fun(runs, x, y))



More information about the Rcpp-commits mailing list