mirror of
https://github.com/cemu-project/vcpkg.git
synced 2025-01-26 21:55:19 +00:00
Introduce ElapsedTime class
This commit is contained in:
parent
8a37cd9b1d
commit
a62558fb79
@ -5,6 +5,25 @@
|
||||
|
||||
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:
|
||||
|
@ -4,6 +4,63 @@
|
||||
|
||||
namespace vcpkg
|
||||
{
|
||||
static std::string format_time_userfriendly(const std::chrono::nanoseconds& nanos)
|
||||
{
|
||||
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;
|
||||
|
||||
const double 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 us", t);
|
||||
}
|
||||
|
||||
return Strings::format("%.4g ns", nanos_as_double);
|
||||
}
|
||||
|
||||
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();
|
||||
@ -45,53 +102,10 @@ namespace vcpkg
|
||||
|
||||
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 us", t);
|
||||
}
|
||||
|
||||
return Strings::format("%.4g ns", nanos_as_double);
|
||||
return format_time_userfriendly(this->elapsedNanos());
|
||||
}
|
||||
|
||||
Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick()
|
||||
{
|
||||
}
|
||||
Stopwatch::Stopwatch() : m_isRunning(false), m_elapsedNanos(0), m_startTick() { }
|
||||
|
||||
std::chrono::nanoseconds Stopwatch::elapsedNanos() const
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user