2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-04-01 22:03:32 +02:00
|
|
|
#include <AK/Assertions.h>
|
2019-05-18 02:00:01 +02:00
|
|
|
#include <AK/Time.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/ElapsedTimer.h>
|
2019-03-25 01:42:15 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
2024-05-08 20:15:05 +02:00
|
|
|
ElapsedTimer ElapsedTimer::start_new(TimerType timer_type)
|
2021-09-12 08:21:16 -07:00
|
|
|
{
|
2024-05-08 20:15:05 +02:00
|
|
|
ElapsedTimer timer(timer_type);
|
2021-09-12 08:21:16 -07:00
|
|
|
timer.start();
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void ElapsedTimer::start()
|
2019-03-25 01:42:15 +01:00
|
|
|
{
|
2019-04-01 22:03:32 +02:00
|
|
|
m_valid = true;
|
2024-02-26 17:52:03 +00:00
|
|
|
m_origin_time = m_timer_type == TimerType::Precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
|
2019-03-25 01:42:15 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 23:41:32 +02:00
|
|
|
void ElapsedTimer::reset()
|
|
|
|
{
|
|
|
|
m_valid = false;
|
|
|
|
}
|
|
|
|
|
2023-05-24 08:32:20 -04:00
|
|
|
i64 ElapsedTimer::elapsed_milliseconds() const
|
2019-03-25 01:42:15 +01:00
|
|
|
{
|
2023-01-01 22:30:31 -07:00
|
|
|
return elapsed_time().to_milliseconds();
|
2019-03-25 01:42:15 +01:00
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
|
2024-07-16 23:43:39 -06:00
|
|
|
AK::Duration ElapsedTimer::elapsed_time() const
|
2021-09-11 20:24:37 -07:00
|
|
|
{
|
2023-01-01 22:30:31 -07:00
|
|
|
VERIFY(is_valid());
|
2024-02-26 17:52:03 +00:00
|
|
|
auto now = m_timer_type == TimerType::Precise ? MonotonicTime::now() : MonotonicTime::now_coarse();
|
2023-01-01 22:30:31 -07:00
|
|
|
return now - m_origin_time;
|
2021-09-11 20:24:37 -07:00
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|