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 */