-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTimer.h
More file actions
57 lines (43 loc) · 1.51 KB
/
Copy pathTimer.h
File metadata and controls
57 lines (43 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef RCPP_TIMER_H
#define RCPP_TIMER_H
namespace Rcpp {
template <typename Clock = std::chrono::high_resolution_clock>
class Timer {
public:
typedef typename Clock::time_point time_point ;
typedef std::pair<std::string,time_point> Step;
typedef std::vector<Step> Steps;
typedef std::chrono::duration<double, std::micro> microseconds ;
Timer(time_point origin_ ) : data(), origin(origin_){}
Timer() : data(), origin(now()) {}
inline void reset(){
data.clear() ;
origin = now() ;
}
inline void step( const std::string& name ){
data.push_back( std::make_pair( name, now() ) ) ;
}
operator SEXP() const {
R_xlen_t n = data.size() ;
NumericVector res(n) ;
CharacterVector names_(n) ;
auto it = data.begin() ;
for( size_t i=0; i<n; i++, ++it){
res[i] = std::chrono::duration_cast<microseconds>( it->second - origin ).count() ;
names_[i] = it->first ;
}
names(res) = names_ ;
return res ;
}
inline time_point now() const {
return Clock::now() ;
}
static std::vector<Timer> get_timers(int n){
return std::vector<Timer>( n, Timer() ) ;
}
private:
Steps data ;
time_point origin ;
} ;
}
#endif