Skip to content

Commit

Permalink
add StopWatch class
Browse files Browse the repository at this point in the history
  • Loading branch information
nam20485 committed Oct 30, 2023
1 parent 00e519c commit cb08601
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CMakeList.txt : CMake project for OdbDesignServer
#

add_library(Utils SHARED "utils_export.h" "ExitCode.h" "ThreadSafeQueue.h" "WorkQueueLoopThread.h" "Logger.h" "Logger.cpp" "CommandLineArgs.h" "CommandLineArgs.cpp" "bin2ascii.h" "ArchiveExtractor.cpp" "ArchiveExtractor.h" "libarchive_extract.cpp" "libarchive_extract.h" "str_trim.cpp" "str_trim.h" "IJsonable.h" "IJsonable.cpp" "CrowReturnable.h" "JsonCrowReturnable.h" "timestamp.h" "timestamp.cpp")
add_library(Utils SHARED "utils_export.h" "ExitCode.h" "ThreadSafeQueue.h" "WorkQueueLoopThread.h" "Logger.h" "Logger.cpp" "CommandLineArgs.h" "CommandLineArgs.cpp" "bin2ascii.h" "ArchiveExtractor.cpp" "ArchiveExtractor.h" "libarchive_extract.cpp" "libarchive_extract.h" "str_trim.cpp" "str_trim.h" "IJsonable.h" "IJsonable.cpp" "CrowReturnable.h" "JsonCrowReturnable.h" "timestamp.h" "timestamp.cpp" "StopWatch.h" "StopWatch.cpp")

# state that anybody linking to us needs to include the current source dir,
# while we don't.
Expand Down
53 changes: 53 additions & 0 deletions Utils/StopWatch.cpp
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;
}
}
34 changes: 34 additions & 0 deletions Utils/StopWatch.h
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;

};
}

0 comments on commit cb08601

Please sign in to comment.