File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ # ##########################################
3+ # Makefile for simple programs
4+ # ##########################################
5+ INC =
6+ LIB =
7+ CC =g++ -std=c++0x
8+ # display all warnings
9+ CC_FLAG =-Wall
10+
11+ PRG =timer_test
12+ OBJ =test.o
13+
14+ $(PRG ) :$(OBJ )
15+ $(CC ) $(INC ) $(LIB ) -o $@ $(OBJ )
16+
17+ .SUFFIXES : .c .o .cpp
18+ .cpp.o :
19+ $(CC ) $(CC_FLAG ) $(INC ) -c $* .cpp -o $* .o
20+
21+ .PRONY :clean
22+ clean :
23+ @echo " Removing linked and compiled files......"
24+ rm -f $(OBJ ) $(PRG )
Original file line number Diff line number Diff line change 1+ #include < thread>
2+ #include < cassert>
3+ #include " timer.h"
4+
5+ bool test_return () {
6+ auto f = [](size_t n) -> int {
7+ std::this_thread::sleep_for (std::chrono::milliseconds (n));
8+ return 0 ;
9+ };
10+ return utility::with_timer (f, 5 ) == 0 ;
11+ }
12+
13+ bool test_void () {
14+ auto f = [](size_t n) {
15+ std::this_thread::sleep_for (std::chrono::milliseconds (n));
16+ };
17+ utility::with_timer (f, 5 );
18+ return true ;
19+ }
20+
21+ int main (int argc, char * argv[]) {
22+ assert (test_return ());
23+ assert (test_void ());
24+ return 0 ;
25+ }
Original file line number Diff line number Diff line change 1+ #include < chrono>
2+ #include < iostream>
3+
4+ namespace utility {
5+ // avoid to work around function with a *void* return value
6+ // use constructor-destructor to calculate time
7+ class TimePrinter {
8+ private:
9+ std::chrono::steady_clock::time_point start_;
10+
11+ public:
12+ TimePrinter () : start_(std::chrono::steady_clock::now()) {}
13+ ~TimePrinter () {
14+ auto end = std::chrono::steady_clock::now ();
15+ std::chrono::duration<double > time_span =
16+ std::chrono::duration_cast<std::chrono::duration<double >>(end - start_);
17+ std::cout << time_span.count () << " seconds elapsed" << std::endl;
18+ }
19+ };
20+
21+ template <typename Func, typename ... Args>
22+ auto with_timer (Func f, Args &&... args)
23+ -> decltype(f(std::forward<Args>(args)...)) {
24+ TimePrinter p;
25+ return f (std::forward<Args>(args)...);
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments