[Rcpp-commits] r4114 - in pkg/Rcpp: . inst/include/Rcpp inst/include/Rcpp/Benchmark src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Dec 8 15:27:33 CET 2012


Author: romain
Date: 2012-12-08 15:27:33 +0100 (Sat, 08 Dec 2012)
New Revision: 4114

Added:
   pkg/Rcpp/inst/include/Rcpp/Benchmark/
   pkg/Rcpp/inst/include/Rcpp/Benchmark/Timer.h
   pkg/Rcpp/src/Timer.cpp
Modified:
   pkg/Rcpp/ChangeLog
Log:
added Benchmark::Timer

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-12-07 18:43:28 UTC (rev 4113)
+++ pkg/Rcpp/ChangeLog	2012-12-08 14:27:33 UTC (rev 4114)
@@ -1,3 +1,9 @@
+2012-12-08 Romain Francois <romain at r-enthusiasts.com>
+
+        * src/Timer.cpp: implementation of Timer
+        * include/Rcpp/Benchmark/Timer.h: internal performance timer, based on
+        the code from the microbenchmark package. 
+
 2012-12-07  JJ Allaire <jj at rstudio.org>
 
         * src/AttributesGen.cpp: use __ prefix for variables in generated

Added: pkg/Rcpp/inst/include/Rcpp/Benchmark/Timer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Benchmark/Timer.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/Benchmark/Timer.h	2012-12-08 14:27:33 UTC (rev 4114)
@@ -0,0 +1,54 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Timer.h: Rcpp R/C++ interface class library -- Rcpp benchmark utility
+//
+// Copyright (C) 2012 JJ Allaire, Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <stdint.h>
+#include <vector>
+#include <string>
+
+#define R_NO_REMAP
+#include <Rinternals.h>
+
+namespace Rcpp{
+namespace Benchmark{
+
+    typedef uint64_t nanotime_t;
+    nanotime_t get_nanotime(void); 
+    
+    class Timer {
+    public:
+        Timer() ;
+        
+        void step( const std::string& ) ;
+        
+        operator SEXP() const ;
+        
+        private:
+            typedef std::pair<std::string,nanotime_t> Step;
+            typedef std::vector<Step> Steps;
+            
+            Steps data ;
+            nanotime_t start_time ;
+            
+    } ;
+    
+}
+}
+

Added: pkg/Rcpp/src/Timer.cpp
===================================================================
--- pkg/Rcpp/src/Timer.cpp	                        (rev 0)
+++ pkg/Rcpp/src/Timer.cpp	2012-12-08 14:27:33 UTC (rev 4114)
@@ -0,0 +1,115 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// Timer.cpp: Rcpp R/C++ interface class library -- Rcpp benchmark utility
+//
+// Copyright (C) 2012 JJ Allaire, Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp.h>
+#include <Rcpp/Benchmark/Timer.h>
+
+namespace Rcpp{
+namespace Benchmark{
+     
+      
+#if defined(WIN32) 
+  
+    #include <windows.h>
+    
+    nanotime_t get_nanotime(void) {
+        LARGE_INTEGER time_var, frequency;
+        QueryPerformanceCounter(&time_var);
+        QueryPerformanceFrequency(&frequency);
+        
+        /* Convert to nanoseconds */
+        return 1.0e9 * time_var.QuadPart / frequency.QuadPart;
+    }
+
+#elif defined(__MACH__) || defined(__APPLE__)
+     
+    #include <mach/mach_time.h>
+    nanotime_t get_nanotime(void) {
+        nanotime_t time;
+        mach_timebase_info_data_t info;
+        
+        time = mach_absolute_time();
+        mach_timebase_info(&info);
+        
+        /* Convert to nanoseconds */
+        return time * (info.numer / info.denom);
+    }
+#elif defined(linux) || defined(__linux)
+
+    #include <time.h>
+    static const nanotime_t nanoseconds_in_second = 1000000000LL;
+    
+    nanotime_t get_nanotime(void) {
+        struct timespec time_var;
+    
+        /* Possible other values we could have used are CLOCK_MONOTONIC,
+         * which is takes longer to retrieve and CLOCK_PROCESS_CPUTIME_ID
+         * which, if I understand it correctly, would require the R
+         * process to be bound to one core.
+         */
+        clock_gettime(CLOCK_REALTIME, &time_var);
+    
+        nanotime_t sec = time_var.tv_sec;
+        nanotime_t nsec = time_var.tv_nsec;
+    
+        /* Combine both values to one nanoseconds value */
+        return (nanoseconds_in_second * sec) + nsec;
+    }
+
+#elif defined(sun) || defined(__sun) || defined(_AIX)
+
+    #include <sys/time.h>
+    
+    /* short an sweet! */
+    nanotime_t get_nanotime(void) {
+        return gethrtime();
+    }    
+
+#else /* Unsupported OS */
+    #error "Rcpp::Benchmark::Timer not supported by your OS."
+#endif
+    
+    Timer::Timer() : data(), start_time( get_nanotime() ){}
+    
+    void Timer::step( const std::string& name){
+        nanotime_t now = get_nanotime() ;
+        data.push_back( std::make_pair( name, now - start_time ) ) ;
+        start_time = get_nanotime() ; 
+    }
+      
+    namespace{
+        std::string get_first( const std::pair<std::string,nanotime_t>& pair ){
+            return pair.first ;    
+        }
+        double get_second( const std::pair<std::string,nanotime_t>& pair ){
+            return static_cast<double>(pair.second) ;    
+        }
+    }
+    
+    Timer::operator SEXP() const {
+        NumericVector out( data.begin(), data.end(), get_second ) ; 
+        CharacterVector names( data.begin(), data.end(), get_first ) ;
+        out.attr( "names" ) = names ;
+        return out ;
+    }
+}
+}
+



More information about the Rcpp-commits mailing list