lap_timer.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/timer/lap_timer.h"
  5. #include "base/check_op.h"
  6. namespace base {
  7. namespace {
  8. // Default values.
  9. constexpr TimeDelta kDefaultTimeLimit = Seconds(3);
  10. constexpr int kDefaultWarmupRuns = 5;
  11. constexpr int kDefaultTimeCheckInterval = 10;
  12. } // namespace
  13. LapTimer::LapTimer(int warmup_laps,
  14. TimeDelta time_limit,
  15. int check_interval,
  16. LapTimer::TimerMethod method)
  17. : warmup_laps_(warmup_laps),
  18. time_limit_(time_limit),
  19. check_interval_(check_interval),
  20. method_(method) {
  21. DETACH_FROM_SEQUENCE(sequence_checker_);
  22. DCHECK_GT(check_interval, 0);
  23. Reset();
  24. }
  25. LapTimer::LapTimer(LapTimer::TimerMethod method)
  26. : LapTimer(kDefaultWarmupRuns,
  27. kDefaultTimeLimit,
  28. kDefaultTimeCheckInterval,
  29. method) {}
  30. void LapTimer::Reset() {
  31. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  32. if (ThreadTicks::IsSupported() && method_ == TimerMethod::kUseThreadTicks)
  33. ThreadTicks::WaitUntilInitialized();
  34. num_laps_ = 0;
  35. remaining_warmups_ = warmup_laps_;
  36. remaining_no_check_laps_ = check_interval_;
  37. Start();
  38. }
  39. void LapTimer::Start() {
  40. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  41. DCHECK_EQ(num_laps_, 0);
  42. // last_timed_ variables are initialized here (instead of in the constructor)
  43. // because not all platforms support ThreadTicks.
  44. if (method_ == TimerMethod::kUseThreadTicks) {
  45. start_thread_ticks_ = ThreadTicks::Now();
  46. last_timed_lap_end_thread_ticks_ = ThreadTicks::Now();
  47. } else {
  48. start_time_ticks_ = TimeTicks::Now();
  49. last_timed_lap_end_ticks_ = TimeTicks::Now();
  50. }
  51. }
  52. bool LapTimer::IsWarmedUp() const {
  53. return remaining_warmups_ <= 0;
  54. }
  55. void LapTimer::NextLap() {
  56. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  57. DCHECK(!start_thread_ticks_.is_null() || !start_time_ticks_.is_null());
  58. if (!IsWarmedUp()) {
  59. --remaining_warmups_;
  60. if (IsWarmedUp()) {
  61. Start();
  62. }
  63. return;
  64. }
  65. ++num_laps_;
  66. --remaining_no_check_laps_;
  67. if (!remaining_no_check_laps_) {
  68. if (method_ == TimerMethod::kUseTimeTicks) {
  69. last_timed_lap_end_ticks_ = TimeTicks::Now();
  70. } else {
  71. last_timed_lap_end_thread_ticks_ = ThreadTicks::Now();
  72. }
  73. remaining_no_check_laps_ = check_interval_;
  74. }
  75. }
  76. TimeDelta LapTimer::GetAccumulatedTime() const {
  77. if (method_ == TimerMethod::kUseTimeTicks) {
  78. return last_timed_lap_end_ticks_ - start_time_ticks_;
  79. }
  80. return last_timed_lap_end_thread_ticks_ - start_thread_ticks_;
  81. }
  82. bool LapTimer::HasTimeLimitExpired() const {
  83. return GetAccumulatedTime() >= time_limit_;
  84. }
  85. bool LapTimer::HasTimedAllLaps() const {
  86. return num_laps_ && !(num_laps_ % check_interval_);
  87. }
  88. TimeDelta LapTimer::TimePerLap() const {
  89. DCHECK(HasTimedAllLaps());
  90. DCHECK_GT(num_laps_, 0);
  91. return GetAccumulatedTime() / num_laps_;
  92. }
  93. float LapTimer::LapsPerSecond() const {
  94. DCHECK(HasTimedAllLaps());
  95. DCHECK_GT(num_laps_, 0);
  96. return num_laps_ / GetAccumulatedTime().InSecondsF();
  97. }
  98. int LapTimer::NumLaps() const {
  99. return num_laps_;
  100. }
  101. } // namespace base