make StopWatch more robust

This commit is contained in:
Ingo Weyrich
2019-08-09 15:45:11 +02:00
parent 55a2aae381
commit 63abfb4d66
2 changed files with 14 additions and 29 deletions

View File

@@ -18,10 +18,10 @@
* *
* Author: reine * Author: reine
*/ */
#pragma once
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <iostream> #include <iostream>
#include <string>
#include "mytime.h" #include "mytime.h"
#ifdef BENCHMARK #ifdef BENCHMARK
@@ -36,9 +36,8 @@ class StopWatch
{ {
public: public:
explicit StopWatch( const char* msg, bool microseconds = false ) : microseconds(microseconds) explicit StopWatch(const char* msg, bool microSeconds = false) : message(msg), unit(microSeconds ? " us" : " ms"), divisor(microSeconds ? 1 : 1000)
{ {
message = msg;
start(); start();
stopped = false; stopped = false;
} }
@@ -51,30 +50,20 @@ public:
void start() void start()
{ {
startTime.set(); startTime.set();
}; }
void stop() void stop()
{ {
stopTime.set(); stopTime.set();
if(!microseconds) { const long elapsedTime = stopTime.etime(startTime) / divisor;
long elapsedTime = stopTime.etime(startTime) / 1000; std::cout << message << " took " << elapsedTime << unit << std::endl;
std::cout << message << " took " << elapsedTime << " ms" << std::endl;
} else {
long elapsedTime = stopTime.etime(startTime);
std::cout << message << " took " << elapsedTime << " us" << std::endl;
}
stopped = true; stopped = true;
} }
void stop(const char *msg)
{
message = msg;
stop();
};
private: private:
bool microseconds;
MyTime startTime; MyTime startTime;
MyTime stopTime; MyTime stopTime;
const char *message; const std::string message;
const std::string unit;
const int divisor;
bool stopped; bool stopped;
}; };
#endif /* STOPWATCH_H */

View File

@@ -16,8 +16,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with RawTherapee. If not, see <http://www.gnu.org/licenses/>. * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _MYTIME_ #pragma once
#define _MYTIME_
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
@@ -62,7 +61,7 @@ public:
#endif #endif
} }
int etime (MyTime a) int etime (const MyTime &a) const
{ {
#ifndef WIN32 #ifndef WIN32
return (t.tv_sec - a.t.tv_sec) * 1000000 + (t.tv_nsec - a.t.tv_nsec) / 1000; return (t.tv_sec - a.t.tv_sec) * 1000000 + (t.tv_nsec - a.t.tv_nsec) / 1000;
@@ -71,6 +70,3 @@ public:
#endif #endif
} }
}; };
#endif