Skip to content
Prev 4892 / 10988 Next

[Rcpp-devel] Rcpp internal benchmark tool

Hello,

I've been using the microbenchmark package extensively and took 
inspiration from it to implement a Timer class in Rcpp to measure 
performance at a lower level.

The class is pretty raw right now. platform specific details are 
abstracted out. So the only things needed are:

- include the Timer.h header, which is **not** automatically added in 
Rcpp.h:

     #include <Rcpp/Benchmark/Timer.h>

- create a Timer object :

     Benchmark::Timer timer ;

- call "step" to record each step, measuring the time from the previous 
step to now.

     timer.step( "something" ) ;



Here is a full example measuring a few things:

#include <Rcpp.h>
#include <Rcpp/Benchmark/Timer.h>

using namespace Rcpp ;

// [[Rcpp::export]]
SEXP measure(){
     int n = 1000000 ;

     // start the timer
     Benchmark::Timer timer ;
     for( int i=0; i<n; i++){
          GetRNGstate() ;
          PutRNGstate() ;
     }
     timer.step( "get/put" ) ;
     for( int i=0; i<n; i++){
          GetRNGstate() ;
          rnorm(10, 0.0, 1.0 );
          PutRNGstate() ;
     }
     timer.step( "get/put + rnorm(10, 0,1) " ) ;
     for( int i=0; i<n; i++){}
     timer.step( "empty loop" ) ;
     NumericVector res( timer ) ;
     for( int i=0; i<res.size(); i++) res[i] = res[i] / n ;
     return res ;
}


 > measure()
                   get/put get/put + rnorm(10, 0,1) 
empty loop
               2086.945600               5067.034550 
0.000622

Those numbers are numbers of nanoseconds per iteration of each loop


as I said the class is pretty raw, so if people have ideas to improve it.
See the header: 
https://r-forge.r-project.org/scm/viewvc.php/pkg/Rcpp/inst/include/Rcpp/Benchmark/Timer.h?view=markup&root=rcpp

and the implementation: 
https://r-forge.r-project.org/scm/viewvc.php/pkg/Rcpp/src/Timer.cpp?view=markup&root=rcpp

Romain