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
*/
#pragma once
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <iostream>
#include <string>
#include "mytime.h"
#ifdef BENCHMARK
@@ -36,45 +36,34 @@ class StopWatch
{
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();
stopped = false;
}
~StopWatch()
{
if(!stopped) {
if (!stopped) {
stop();
}
}
void start()
{
startTime.set();
};
}
void stop()
{
stopTime.set();
if(!microseconds) {
long elapsedTime = stopTime.etime(startTime) / 1000;
std::cout << message << " took " << elapsedTime << " ms" << std::endl;
} else {
long elapsedTime = stopTime.etime(startTime);
std::cout << message << " took " << elapsedTime << " us" << std::endl;
}
const long elapsedTime = stopTime.etime(startTime) / divisor;
std::cout << message << " took " << elapsedTime << unit << std::endl;
stopped = true;
}
void stop(const char *msg)
{
message = msg;
stop();
};
private:
bool microseconds;
MyTime startTime;
MyTime stopTime;
const char *message;
const std::string message;
const std::string unit;
const int divisor;
bool stopped;
};
#endif /* STOPWATCH_H */

View File

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