Created
October 16, 2017 07:18
-
-
Save arxeiss/540e975b96b0d2c70c2be96a28da07e9 to your computer and use it in GitHub Desktop.
Simple class for elapsed time measuring in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <unordered_map> | |
#include <chrono> | |
class TimeMeasuring | |
{ | |
private: | |
bool started = false; | |
std::chrono::steady_clock::time_point start; | |
std::unordered_map<std::string, std::chrono::steady_clock::time_point> breakpoints; | |
public: | |
TimeMeasuring(bool start = false) { | |
this->breakpoints = std::unordered_map<std::string, std::chrono::steady_clock::time_point>(); | |
if (start) | |
{ | |
this->startMeasuring(); | |
} | |
} | |
bool startMeasuring() { | |
if (!this->started) | |
{ | |
this->start = std::chrono::steady_clock::now(); | |
this->started = true; | |
return true; | |
} | |
return false; | |
} | |
bool insertBreakpoint(std::string name) { | |
if (this->breakpoints.count(name) == 0) | |
{ | |
this->breakpoints[name] = std::chrono::steady_clock::now(); | |
return true; | |
} | |
return false; | |
} | |
long long int getTimeFromBeginning(bool microSeconds = false) { | |
if (!this->started) | |
{ | |
return -1; | |
} | |
if (microSeconds) | |
{ | |
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - this->start).count(); | |
} | |
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - this->start).count(); | |
} | |
long long int getTimeFromBreakpoint(std::string breakpoint, bool microSeconds = false) { | |
if (!this->started || this->breakpoints.count(breakpoint) == 0) | |
{ | |
return -1; | |
} | |
if (microSeconds) | |
{ | |
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - this->breakpoints[breakpoint]).count(); | |
} | |
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - this->breakpoints[breakpoint]).count(); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment