Extended StopWatch class to report processing time in microseconds

This commit is contained in:
heckflosse
2016-10-05 01:15:52 +02:00
parent 18ee91b8e4
commit 8a2b2e6700

View File

@@ -26,18 +26,17 @@
#ifdef BENCHMARK #ifdef BENCHMARK
#define BENCHFUN StopWatch StopFun(__func__); #define BENCHFUN StopWatch StopFun(__func__);
#define BENCHFUNMICRO StopWatch StopFun(__func__, true);
#else #else
#define BENCHFUN #define BENCHFUN
#define BENCHFUNMICRO
#endif #endif
class StopWatch class StopWatch
{ {
public: public:
StopWatch( )
{ explicit StopWatch( const char* msg, bool microseconds = false ) : microseconds(microseconds)
stopped = false;
}
explicit StopWatch( const char* msg )
{ {
message = msg; message = msg;
start(); start();
@@ -56,8 +55,13 @@ public:
void stop() void stop()
{ {
stopTime.set(); stopTime.set();
long elapsedTime = stopTime.etime(startTime) / 1000; if(!microseconds) {
std::cout << message << " took " << elapsedTime << " ms" << std::endl; 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;
}
stopped = true; stopped = true;
} }
void stop(const char *msg) void stop(const char *msg)
@@ -66,6 +70,7 @@ public:
stop(); stop();
}; };
private: private:
bool microseconds;
MyTime startTime; MyTime startTime;
MyTime stopTime; MyTime stopTime;
const char *message; const char *message;