Skip to content

Commit a2c3191

Browse files
tools to help with Timer.
1 parent 12b9cf1 commit a2c3191

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

inst/include/RcppParallel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "RcppParallel/RVector.h"
1919
#include "RcppParallel/RMatrix.h"
20+
#include <RcppParallel/Timer.h>
2021

2122
namespace RcppParallel {
2223

inst/include/RcppParallel/Timer.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#ifndef __RCPP_PARALLEL_TIMER__
2+
#define __RCPP_PARALLEL_TIMER__
3+
4+
namespace RcppParallel {
5+
6+
template <typename Timer>
7+
class SingleTimer {
8+
public:
9+
SingleTimer() : timer(){}
10+
11+
inline operator SEXP(){
12+
Rcpp::List out = Rcpp::List::create(timer) ;
13+
out.attr("class") = Rcpp::CharacterVector::create( "SingleTimer", "Timer" );
14+
return out ;
15+
}
16+
17+
inline void step( const char* name ){
18+
timer.step(name) ;
19+
}
20+
21+
private:
22+
Timer timer ;
23+
} ;
24+
25+
template <typename Timer>
26+
class FixedSizeTimers {
27+
public:
28+
FixedSizeTimers( int n ) : timers(n){}
29+
30+
inline Timer& get(int i) {
31+
return timers[i] ;
32+
}
33+
34+
inline operator SEXP(){
35+
Rcpp::List out = wrap(timers) ;
36+
out.attr("class") = Rcpp::CharacterVector::create("FixedSizeTimers", "Timer") ;
37+
return out ;
38+
}
39+
40+
41+
private:
42+
std::vector<Timer> timers ;
43+
} ;
44+
45+
template <typename Timer, typename Mutex, typename Locker>
46+
class TimersList {
47+
public:
48+
49+
TimersList(): timers(){}
50+
51+
Timer& get_new_timer(){
52+
Locker lock(mutex) ;
53+
if( timers.empty() ) {
54+
timers.push_back( Timer() ) ;
55+
} else {
56+
timers.push_back( Timer( timers.front().origin() ) ) ;
57+
}
58+
std::cout << timers.size() << " timers" << std::endl ;
59+
return timers.back() ;
60+
}
61+
62+
inline operator SEXP(){
63+
Rcpp::List data = wrap(timers) ;
64+
data.attr("class") = Rcpp::CharacterVector::create( "TimersList" , "Timer" ) ;
65+
return data ;
66+
}
67+
68+
private:
69+
TimersList( const TimersList& ) ;
70+
std::list<Timer> timers ;
71+
Mutex mutex ;
72+
73+
} ;
74+
75+
76+
} // namespace RcppParallel
77+
78+
79+
#endif // __RCPP_PARALLEL_COMMON__

0 commit comments

Comments
 (0)