diff options
| -rw-r--r-- | toolsrc/include/Stopwatch.h | 40 | ||||
| -rw-r--r-- | toolsrc/src/Stopwatch.cpp | 104 | ||||
| -rw-r--r-- | toolsrc/vcpkgcommon/vcpkgcommon.vcxproj | 2 | ||||
| -rw-r--r-- | toolsrc/vcpkgcommon/vcpkgcommon.vcxproj.filters | 6 |
4 files changed, 152 insertions, 0 deletions
diff --git a/toolsrc/include/Stopwatch.h b/toolsrc/include/Stopwatch.h new file mode 100644 index 000000000..2bd5c31bf --- /dev/null +++ b/toolsrc/include/Stopwatch.h @@ -0,0 +1,40 @@ +#pragma once + +#include <chrono> +#include <string> + +namespace vcpkg +{ + class Stopwatch + { + public: + static Stopwatch createUnstarted(); + + static Stopwatch createStarted(); + + bool isRunning() const; + + Stopwatch& start(); + + Stopwatch& stop(); + + Stopwatch& reset(); + + template <class TimeUnit> + TimeUnit elapsed() const + { + return std::chrono::duration_cast<TimeUnit>(elapsedNanos()); + } + + std::string toString() const; + + private: + Stopwatch(); + + std::chrono::nanoseconds elapsedNanos() const; + + bool m_isRunning; + std::chrono::nanoseconds m_elapsedNanos; + std::chrono::steady_clock::time_point m_startTick; + }; +} diff --git a/toolsrc/src/Stopwatch.cpp b/toolsrc/src/Stopwatch.cpp new file mode 100644 index 000000000..035d54c2b --- /dev/null +++ b/toolsrc/src/Stopwatch.cpp @@ -0,0 +1,104 @@ +#include "Stopwatch.h" +#include "vcpkg_Checks.h" + +namespace vcpkg +{ + Stopwatch Stopwatch::createUnstarted() + { + return Stopwatch(); + } + + Stopwatch Stopwatch::createStarted() + { + return Stopwatch().start(); + } + + bool Stopwatch::isRunning() const + { + return this->m_isRunning; + } + + 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; + } + + 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 + { + using std::chrono::hours; + using std::chrono::minutes; + using std::chrono::seconds; + using std::chrono::milliseconds; + using std::chrono::microseconds; + using std::chrono::nanoseconds; + using std::chrono::duration_cast; + + auto nanos = elapsedNanos(); + auto nanos_as_double = static_cast<double>(nanos.count()); + + if (duration_cast<hours>(nanos) > hours()) + { + auto t = nanos_as_double / duration_cast<nanoseconds>(hours(1)).count(); + return Strings::format("%.4g h", t); + } + + if (duration_cast<minutes>(nanos) > minutes()) + { + auto t = nanos_as_double / duration_cast<nanoseconds>(minutes(1)).count(); + return Strings::format("%.4g min", t); + } + + if (duration_cast<seconds>(nanos) > seconds()) + { + auto t = nanos_as_double / duration_cast<nanoseconds>(seconds(1)).count(); + return Strings::format("%.4g s", t); + } + + if (duration_cast<milliseconds>(nanos) > milliseconds()) + { + auto t = nanos_as_double / duration_cast<nanoseconds>(milliseconds(1)).count(); + return Strings::format("%.4g ms", t); + } + + if (duration_cast<microseconds>(nanos) > microseconds()) + { + auto t = nanos_as_double / duration_cast<nanoseconds>(microseconds(1)).count(); + return Strings::format("%.4g micros", t); + } + + return Strings::format("%.4g ns", nanos_as_double); + } + + Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(), m_startTick() + { + } + + std::chrono::nanoseconds Stopwatch::elapsedNanos() const + { + if (this->m_isRunning) + { + return std::chrono::high_resolution_clock::now() - this->m_startTick + this->m_elapsedNanos; + } + + return this->m_elapsedNanos; + } +} diff --git a/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj b/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj index d5e68fde3..218a826ad 100644 --- a/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj +++ b/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj @@ -120,6 +120,7 @@ </Link> </ItemDefinitionGroup> <ItemGroup> + <ClCompile Include="..\src\Stopwatch.cpp" /> <ClCompile Include="..\src\vcpkg_Checks.cpp" /> <ClCompile Include="..\src\vcpkg_Files.cpp" /> <ClCompile Include="..\src\vcpkg_Strings.cpp" /> @@ -128,6 +129,7 @@ <ItemGroup> <ClInclude Include="..\include\expected.h" /> <ClInclude Include="..\include\opt_bool.h" /> + <ClInclude Include="..\include\Stopwatch.h" /> <ClInclude Include="..\include\vcpkg_Checks.h" /> <ClInclude Include="..\include\vcpkg_Files.h" /> <ClInclude Include="..\include\vcpkg_Graphs.h" /> diff --git a/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj.filters b/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj.filters index ae747d687..4d40bfbe2 100644 --- a/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj.filters +++ b/toolsrc/vcpkgcommon/vcpkgcommon.vcxproj.filters @@ -27,6 +27,9 @@ <ClCompile Include="..\src\vcpkg_Files.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="..\src\Stopwatch.cpp"> + <Filter>Source Files</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\include\vcpkg_Checks.h"> @@ -56,5 +59,8 @@ <ClInclude Include="..\include\vcpkg_Sets.h"> <Filter>Header Files</Filter> </ClInclude> + <ClInclude Include="..\include\Stopwatch.h"> + <Filter>Header Files</Filter> + </ClInclude> </ItemGroup> </Project>
\ No newline at end of file |
