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