#pragma once
#include <cmath>
#include <wtf/CurrentTime.h>
#include <wtf/MonotonicTime.h>
#include <wtf/RefCounted.h>
namespace WTF {
class Stopwatch : public RefCounted<Stopwatch> {
public:
static Ref<Stopwatch> create()
{
return adoptRef(*new Stopwatch());
}
void reset();
void start();
void stop();
double elapsedTime();
double elapsedTimeSince(MonotonicTime);
bool isActive() const { return !std::isnan(m_lastStartTime); }
private:
Stopwatch() { reset(); }
double m_elapsedTime;
double m_lastStartTime;
};
inline void Stopwatch::reset()
{
m_elapsedTime = 0.0;
m_lastStartTime = NAN;
}
inline void Stopwatch::start()
{
ASSERT_WITH_MESSAGE(std::isnan(m_lastStartTime), "Tried to start the stopwatch, but it is already running.");
m_lastStartTime = monotonicallyIncreasingTime();
}
inline void Stopwatch::stop()
{
ASSERT_WITH_MESSAGE(!std::isnan(m_lastStartTime), "Tried to stop the stopwatch, but it is not running.");
m_elapsedTime += monotonicallyIncreasingTime() - m_lastStartTime;
m_lastStartTime = NAN;
}
inline double Stopwatch::elapsedTime()
{
if (!isActive())
return m_elapsedTime;
return m_elapsedTime + (monotonicallyIncreasingTime() - m_lastStartTime);
}
inline double Stopwatch::elapsedTimeSince(MonotonicTime timeStamp)
{
if (!isActive())
return m_elapsedTime;
return m_elapsedTime + (timeStamp.secondsSinceEpoch().seconds() - m_lastStartTime);
}
}
using WTF::Stopwatch;