Skip to content

[Rcpp-devel] Rcpp internal benchmark tool

3 messages · Romain Francois, Dirk Eddelbuettel

#
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
#
On 8 December 2012 at 15:44, Romain Francois wrote:
| 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.

That's probably quite useful. I had an older/simpler class here from
something I had done years ago at work covering the Linux + Windows
cases. Olaf has more basis covered so this good.

It is on the border of creeping featuritis though ...
 
| - include the Timer.h header, which is **not** automatically added in 
| Rcpp.h:

... but that is a fair defence against bloat.

Dirk
#
Le 08/12/12 16:06, Dirk Eddelbuettel a ?crit :
precisely. for new features that are somewhat orthogonal to the api, we 
should separate. Rcpp.h is already quite heavy as it is now.