2019-04-10 17:14:44 +03:00
|
|
|
#include <LibCore/CElapsedTimer.h>
|
2019-04-01 23:03:32 +03:00
|
|
|
#include <AK/Assertions.h>
|
2019-03-27 03:31:53 +03:00
|
|
|
#include <sys/time.h>
|
2019-03-25 03:42:15 +03:00
|
|
|
|
2019-04-10 17:14:44 +03:00
|
|
|
void CElapsedTimer::start()
|
2019-03-25 03:42:15 +03:00
|
|
|
{
|
2019-04-01 23:03:32 +03:00
|
|
|
m_valid = true;
|
2019-03-25 03:42:15 +03:00
|
|
|
gettimeofday(&m_start_time, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void timersub(const struct timeval* a, const struct timeval* b, struct timeval* result)
|
|
|
|
{
|
|
|
|
result->tv_sec = a->tv_sec - b->tv_sec;
|
|
|
|
result->tv_usec = a->tv_usec - b->tv_usec;
|
|
|
|
if (result->tv_usec < 0) {
|
|
|
|
--result->tv_sec;
|
|
|
|
result->tv_usec += 1000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:14:44 +03:00
|
|
|
int CElapsedTimer::elapsed() const
|
2019-03-25 03:42:15 +03:00
|
|
|
{
|
2019-04-01 23:03:32 +03:00
|
|
|
ASSERT(is_valid());
|
2019-03-25 03:42:15 +03:00
|
|
|
struct timeval now;
|
|
|
|
gettimeofday(&now, nullptr);
|
|
|
|
struct timeval diff;
|
|
|
|
timersub(&now, &m_start_time, &diff);
|
|
|
|
return diff.tv_sec * 1000 + diff.tv_usec / 1000;
|
|
|
|
}
|