-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "StopWatch.h" | ||
#include <sstream> | ||
#include <iomanip> | ||
|
||
using namespace std::chrono; | ||
|
||
namespace Utils | ||
{ | ||
StopWatch::StopWatch(bool start) | ||
: m_stopped(false) | ||
{ | ||
if (start) | ||
{ | ||
this->start(); | ||
} | ||
} | ||
|
||
void StopWatch::start() | ||
{ | ||
m_stopped = false; | ||
m_started = system_clock::now(); | ||
} | ||
|
||
void StopWatch::stop() | ||
{ | ||
m_finished = system_clock::now(); | ||
m_stopped = true; | ||
} | ||
|
||
long long StopWatch::getElapsedMilliseconds() const | ||
{ | ||
return duration_cast<milliseconds>(getElapsedDuration()).count(); | ||
} | ||
|
||
double StopWatch::getElapsedSeconds() const | ||
{ | ||
return getElapsedMilliseconds() / MS_PER_SECOND; | ||
} | ||
|
||
std::string StopWatch::getElapsedSecondsString(const std::string& suffix) const | ||
{ | ||
std::stringstream ss; | ||
ss << std::fixed << std::setprecision(3) << getElapsedSeconds() << suffix; | ||
return ss.str(); | ||
} | ||
|
||
system_clock::duration StopWatch::getElapsedDuration() const | ||
{ | ||
// if stop() hasn't been called yet, use the current time | ||
if (m_stopped) return m_finished - m_started; | ||
else return system_clock::now() - m_started; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include "utils_export.h" | ||
|
||
namespace Utils | ||
{ | ||
class UTILS_EXPORT StopWatch | ||
{ | ||
public: | ||
StopWatch(bool start); | ||
|
||
void start(); | ||
void stop(); | ||
|
||
long long getElapsedMilliseconds() const; | ||
double getElapsedSeconds() const; | ||
std::string getElapsedSecondsString(const std::string& suffix) const; | ||
std::chrono::system_clock::duration getElapsedDuration() const; | ||
|
||
//template<class D> | ||
//std::chrono::system_clock::duration getElapsedDuration() const; | ||
// | ||
|
||
constexpr inline static const double MS_PER_SECOND = 1000.0; | ||
|
||
private: | ||
std::chrono::system_clock::time_point m_started; | ||
std::chrono::system_clock::time_point m_finished; | ||
|
||
bool m_stopped; | ||
|
||
}; | ||
} |