[Rcpp-commits] r2281 - tests/rcppFunctionEval

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 7 01:34:22 CEST 2010


Author: edd
Date: 2010-10-07 01:34:20 +0200 (Thu, 07 Oct 2010)
New Revision: 2281

Modified:
   tests/rcppFunctionEval/evaluate2.cpp
   tests/rcppFunctionEval/evaluate3.cpp
   tests/rcppFunctionEval/evaluate4.cpp
   tests/rcppFunctionEval/evaluate5.cpp
Log:
add some colour to functions to describe differences


Modified: tests/rcppFunctionEval/evaluate2.cpp
===================================================================
--- tests/rcppFunctionEval/evaluate2.cpp	2010-10-06 22:18:31 UTC (rev 2280)
+++ tests/rcppFunctionEval/evaluate2.cpp	2010-10-06 23:34:20 UTC (rev 2281)
@@ -1,7 +1,8 @@
 
 #include <Rcpp.h>
 
-/*------General functions-----------------------------------------*/
+// similar to evaluate1 from DEoptim but with Rcpp objects for function 
+// and environment -- considerably slower
 
 RcppExport double evaluate2(long *l_nfeval, 
 			    double *param, int i_D,

Modified: tests/rcppFunctionEval/evaluate3.cpp
===================================================================
--- tests/rcppFunctionEval/evaluate3.cpp	2010-10-06 22:18:31 UTC (rev 2280)
+++ tests/rcppFunctionEval/evaluate3.cpp	2010-10-06 23:34:20 UTC (rev 2281)
@@ -1,7 +1,8 @@
 
 #include <Rcpp.h>
 
-/*------General functions-----------------------------------------*/
+// similar to evaluate1 from DEoptim but with Rcpp objects for function 
+// and environment in the signature too -- considerably slower
 
 RcppExport double evaluate3(long *l_nfeval, 
 			    double *param, int i_D,

Modified: tests/rcppFunctionEval/evaluate4.cpp
===================================================================
--- tests/rcppFunctionEval/evaluate4.cpp	2010-10-06 22:18:31 UTC (rev 2280)
+++ tests/rcppFunctionEval/evaluate4.cpp	2010-10-06 23:34:20 UTC (rev 2281)
@@ -1,6 +1,10 @@
  
 #include <Rcpp.h>
 
+// similar to evaluate1 from DEoptim but with Rcpp objects for function 
+// and environment in the signature too, plus funcall.eval(env) 
+// -- still considerably slower
+
 RcppExport double evaluate4(long *l_nfeval, 
 			    double *param, int i_D,
 			    const Rcpp::Function & fun, const Rcpp::Environment & env)

Modified: tests/rcppFunctionEval/evaluate5.cpp
===================================================================
--- tests/rcppFunctionEval/evaluate5.cpp	2010-10-06 22:18:31 UTC (rev 2280)
+++ tests/rcppFunctionEval/evaluate5.cpp	2010-10-06 23:34:20 UTC (rev 2281)
@@ -1,38 +1,22 @@
 
 #include <Rcpp.h>
 
-// version from DEoptim
+// faster hybrid version:
+//  -- uses reference for l_nfeval
+//  -- uses NumericVector for par, no copying or alloc needed
+//  -- uses SEXPs for function and env
 
-//------objective function---------------------------------------
-
 RcppExport double evaluate5(long & l_nfeval, Rcpp::NumericVector & par, SEXP fcall, SEXP env)
 {
-#if 0 
-  // with PROTECT
-  SEXP sexp_fvec, fn;
-  double f_result;  
-  
-  PROTECT(fn = Rf_lang2(fcall, Rcpp::wrap(par)));
   l_nfeval++;  //increment function evaluation count
-  
-  SETCADR(fn, par);
- 
-  PROTECT(sexp_fvec = Rf_eval(fn, env));
-  f_result = Rcpp::as<double>(sexp_fvec);
- 
-  UNPROTECT(2);	
- #endif
 
-  // without PROTECT -- safe enough?
+  // without PROTECT -- safe enough as we do not return fn or sexp_fvec
   SEXP fn = Rf_lang2(fcall, Rcpp::wrap(par));
-  l_nfeval++;  //increment function evaluation count
-  
   SETCADR(fn, par);
- 
   SEXP sexp_fvec = Rf_eval(fn, env);
+
   double f_result = Rcpp::as<double>(sexp_fvec);
- 
-  if(ISNAN(f_result))
+  if (ISNAN(f_result))
     Rf_error("NaN value of objective function! \nPerhaps adjust the bounds.");
    
   return(f_result); 



More information about the Rcpp-commits mailing list