1 /*
2  *  This file is part of RawTherapee.
3  *
4  *  Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
5  *
6  *  RawTherapee is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  RawTherapee is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *  Author: reine
20  */
21 
22 #ifndef STOPWATCH_H
23 #define STOPWATCH_H
24 #include <iostream>
25 #include "mytime.h"
26 
27 #ifdef BENCHMARK
28     #define BENCHFUN StopWatch StopFun(__func__);
29     #define BENCHFUNMICRO StopWatch StopFun(__func__, true);
30 #else
31     #define BENCHFUN
32     #define BENCHFUNMICRO
33 #endif
34 
35 class StopWatch
36 {
37 public:
38 
microseconds(microseconds)39     explicit StopWatch( const char* msg, bool microseconds = false ) : microseconds(microseconds)
40     {
41         message = msg;
42         start();
43         stopped = false;
44     }
~StopWatch()45     ~StopWatch()
46     {
47         if(!stopped) {
48             stop();
49         }
50     }
start()51     void start()
52     {
53         startTime.set();
54     };
stop()55     void stop()
56     {
57         stopTime.set();
58         if(!microseconds) {
59             long elapsedTime = stopTime.etime(startTime) / 1000;
60             std::cout << message << " took " << elapsedTime << " ms" << std::endl;
61         } else {
62             long elapsedTime = stopTime.etime(startTime);
63             std::cout << message << " took " << elapsedTime << " us" << std::endl;
64         }
65         stopped = true;
66     }
stop(const char * msg)67     void stop(const char *msg)
68     {
69         message = msg;
70         stop();
71     };
72 private:
73     bool microseconds;
74     MyTime startTime;
75     MyTime stopTime;
76     const char *message;
77     bool stopped;
78 };
79 
80 #endif  /* STOPWATCH_H */
81