make StopWatch more robust
This commit is contained in:
@@ -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,45 +36,34 @@ 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;
|
||||||
}
|
}
|
||||||
~StopWatch()
|
~StopWatch()
|
||||||
{
|
{
|
||||||
if(!stopped) {
|
if (!stopped) {
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 */
|
|
||||||
|
@@ -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>
|
||||||
@@ -58,11 +57,11 @@ public:
|
|||||||
t.tv_sec = tv.tv_sec;
|
t.tv_sec = tv.tv_sec;
|
||||||
t.tv_nsec = tv.tv_usec * 1000;
|
t.tv_nsec = tv.tv_usec * 1000;
|
||||||
#else
|
#else
|
||||||
clock_gettime (CLOCK_REALTIME, &t);
|
clock_gettime(CLOCK_REALTIME, &t);
|
||||||
#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
|
|
||||||
|
Reference in New Issue
Block a user