|
2 | 2 | // |
3 | 3 | // Timer.h: Rcpp R/C++ interface class library -- Rcpp benchmark utility |
4 | 4 | // |
5 | | -// Copyright (C) 2012 JJ Allaire, Dirk Eddelbuettel and Romain Francois |
| 5 | +// Copyright (C) 2012 - 2013 JJ Allaire, Dirk Eddelbuettel and Romain Francois |
6 | 6 | // |
7 | 7 | // This file is part of Rcpp. |
8 | 8 | // |
|
29 | 29 | #define R_NO_REMAP |
30 | 30 | #include <Rinternals.h> |
31 | 31 |
|
| 32 | +#if defined(_WIN32) |
| 33 | + #define WIN32_LEAN_AND_MEAN |
| 34 | + #include <windows.h> |
| 35 | +#elif defined(__APPLE__) |
| 36 | + #include <mach/mach_time.h> |
| 37 | +#elif defined(linux) || defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__GLIBC__) || defined(__GNU__) |
| 38 | + #include <time.h> |
| 39 | +#elif defined(sun) || defined(__sun) || defined(_AIX) |
| 40 | + #include <sys/time.h> |
| 41 | +#else /* Unsupported OS */ |
| 42 | + #error "Rcpp::Timer not supported by your OS." |
| 43 | +#endif |
| 44 | + |
32 | 45 | namespace Rcpp{ |
33 | 46 |
|
34 | 47 | typedef uint64_t nanotime_t; |
35 | | - nanotime_t get_nanotime(void); |
| 48 | + |
| 49 | +#if defined(_WIN32) |
| 50 | + |
| 51 | + inline nanotime_t get_nanotime(void) { |
| 52 | + LARGE_INTEGER time_var, frequency; |
| 53 | + QueryPerformanceCounter(&time_var); |
| 54 | + QueryPerformanceFrequency(&frequency); |
| 55 | + |
| 56 | + /* Convert to nanoseconds */ |
| 57 | + return 1.0e9 * time_var.QuadPart / frequency.QuadPart; |
| 58 | + } |
| 59 | + |
| 60 | +#elif defined(__APPLE__) |
| 61 | + |
| 62 | + inline nanotime_t get_nanotime(void) { |
| 63 | + nanotime_t time; |
| 64 | + mach_timebase_info_data_t info; |
| 65 | + |
| 66 | + time = mach_absolute_time(); |
| 67 | + mach_timebase_info(&info); |
| 68 | + |
| 69 | + /* Convert to nanoseconds */ |
| 70 | + return time * (info.numer / info.denom); |
| 71 | + } |
| 72 | + |
| 73 | +#elif defined(linux) || defined(__linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__GLIBC__) || defined(__GNU__) |
| 74 | + |
| 75 | + static const nanotime_t nanoseconds_in_second = static_cast<nanotime_t>(1000000000.0); |
| 76 | + |
| 77 | + inline nanotime_t get_nanotime(void) { |
| 78 | + struct timespec time_var; |
| 79 | + |
| 80 | + /* Possible other values we could have used are CLOCK_MONOTONIC, |
| 81 | + * which is takes longer to retrieve and CLOCK_PROCESS_CPUTIME_ID |
| 82 | + * which, if I understand it correctly, would require the R |
| 83 | + * process to be bound to one core. |
| 84 | + */ |
| 85 | + clock_gettime(CLOCK_REALTIME, &time_var); |
| 86 | + |
| 87 | + nanotime_t sec = time_var.tv_sec; |
| 88 | + nanotime_t nsec = time_var.tv_nsec; |
| 89 | + |
| 90 | + /* Combine both values to one nanoseconds value */ |
| 91 | + return (nanoseconds_in_second * sec) + nsec; |
| 92 | + } |
| 93 | + |
| 94 | +#elif defined(sun) || defined(__sun) || defined(_AIX) |
| 95 | + |
| 96 | + /* short an sweet! */ |
| 97 | + inline nanotime_t get_nanotime(void) { |
| 98 | + return gethrtime(); |
| 99 | + } |
| 100 | + |
| 101 | +#endif |
| 102 | + |
36 | 103 |
|
37 | 104 | class Timer { |
38 | 105 | public: |
39 | | - Timer() ; |
| 106 | + Timer() : data(), start_time( get_nanotime() ){} |
40 | 107 |
|
41 | | - void step( const std::string& ) ; |
| 108 | + void step( const std::string& name){ |
| 109 | + nanotime_t now = get_nanotime() ; |
| 110 | + data.push_back( std::make_pair( name, now - start_time ) ) ; |
| 111 | + start_time = get_nanotime() ; |
| 112 | + } |
42 | 113 |
|
43 | | - operator SEXP() const ; |
| 114 | + operator SEXP() const { |
| 115 | + NumericVector out( data.begin(), data.end(), get_second ) ; |
| 116 | + CharacterVector names( data.begin(), data.end(), get_first ) ; |
| 117 | + out.attr( "names" ) = names ; |
| 118 | + return out ; |
| 119 | + } |
44 | 120 |
|
45 | | - private: |
46 | | - typedef std::pair<std::string,nanotime_t> Step; |
47 | | - typedef std::vector<Step> Steps; |
48 | | - |
49 | | - Steps data ; |
50 | | - nanotime_t start_time ; |
| 121 | + private: |
| 122 | + |
| 123 | + inline std::string get_first( const std::pair<std::string,nanotime_t>& pair ){ |
| 124 | + return pair.first ; |
| 125 | + } |
| 126 | + inline double get_second( const std::pair<std::string,nanotime_t>& pair ){ |
| 127 | + return static_cast<double>(pair.second) ; |
| 128 | + } |
| 129 | + |
| 130 | + typedef std::pair<std::string,nanotime_t> Step; |
| 131 | + typedef std::vector<Step> Steps; |
| 132 | + |
| 133 | + Steps data ; |
| 134 | + nanotime_t start_time ; |
51 | 135 |
|
52 | 136 | } ; |
53 | 137 |
|
|
0 commit comments