Introduce ElapsedTime class
MODIFY
include/Stopwatch.h
include/Stopwatch.h
namespace vcpkg
{
+ class ElapsedTime
+ {
+ public:
+ static ElapsedTime createStarted();
+
+ constexpr ElapsedTime() :m_startTick() {}
+
+ template <class TimeUnit>
+ TimeUnit elapsed() const
+ {
+ return std::chrono::duration_cast<TimeUnit>(std::chrono::high_resolution_clock::now() - this->m_startTick);
+ }
+
+ std::string toString() const;
+
+ private:
+ std::chrono::steady_clock::time_point m_startTick;
+ };
+
class Stopwatch
{
public:

MODIFY
src/Stopwatch.cpp
src/Stopwatch.cpp
namespace vcpkg
{
- Stopwatch Stopwatch::createUnstarted()
- {
- return Stopwatch();
- }
-
- Stopwatch Stopwatch::createStarted()
- {
- return Stopwatch().start();
- }
-
- bool Stopwatch::isRunning() const
- {
- return this->m_isRunning;
- }
-
- const Stopwatch& Stopwatch::start()
- {
- Checks::check_exit(!this->m_isRunning, "This stopwatch is already running.");
- this->m_isRunning = true;
- this->m_startTick = std::chrono::high_resolution_clock::now();
- return *this;
- }
-
- const Stopwatch& Stopwatch::stop()
- {
- auto tick = std::chrono::high_resolution_clock::now();
- Checks::check_exit(this->m_isRunning, "This stopwatch is already stopped.");
- this->m_isRunning = false;
- this->m_elapsedNanos += tick - this->m_startTick;
- return *this;
- }
-
- Stopwatch& Stopwatch::reset()
- {
- this->m_elapsedNanos = std::chrono::nanoseconds();
- this->m_isRunning = false;
- return *this;
- }
-
- std::string Stopwatch::toString() const
+ static std::string format_time_userfriendly(const std::chrono::nanoseconds& nanos)
{
using std::chrono::hours;
using std::chrono::minutes;
@@ -53,8 +14,7 @@ namespace vcpkg
using std::chrono::nanoseconds;
using std::chrono::duration_cast;
- auto nanos = elapsedNanos();
- auto nanos_as_double = static_cast<double>(nanos.count());
+ const double nanos_as_double = static_cast<double>(nanos.count());
if (duration_cast<hours>(nanos) > hours())
{
@@ -89,10 +49,64 @@ namespace vcpkg
return Strings::format("%.4g ns", nanos_as_double);
}
- Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick()
+ ElapsedTime ElapsedTime::createStarted()
{
+ ElapsedTime t;
+ t.m_startTick = std::chrono::high_resolution_clock::now();
+ return t;
}
+ std::string ElapsedTime::toString() const
+ {
+ return format_time_userfriendly(elapsed<std::chrono::nanoseconds>());
+ }
+
+ Stopwatch Stopwatch::createUnstarted()
+ {
+ return Stopwatch();
+ }
+
+ Stopwatch Stopwatch::createStarted()
+ {
+ return Stopwatch().start();
+ }
+
+ bool Stopwatch::isRunning() const
+ {
+ return this->m_isRunning;
+ }
+
+ const Stopwatch& Stopwatch::start()
+ {
+ Checks::check_exit(!this->m_isRunning, "This stopwatch is already running.");
+ this->m_isRunning = true;
+ this->m_startTick = std::chrono::high_resolution_clock::now();
+ return *this;
+ }
+
+ const Stopwatch& Stopwatch::stop()
+ {
+ auto tick = std::chrono::high_resolution_clock::now();
+ Checks::check_exit(this->m_isRunning, "This stopwatch is already stopped.");
+ this->m_isRunning = false;
+ this->m_elapsedNanos += tick - this->m_startTick;
+ return *this;
+ }
+
+ Stopwatch& Stopwatch::reset()
+ {
+ this->m_elapsedNanos = std::chrono::nanoseconds();
+ this->m_isRunning = false;
+ return *this;
+ }
+
+ std::string Stopwatch::toString() const
+ {
+ return format_time_userfriendly(this->elapsedNanos());
+ }
+
+ Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick() { }
+
std::chrono::nanoseconds Stopwatch::elapsedNanos() const
{
if (this->m_isRunning)

Introduce ElapsedTime class