| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <cmath> | 7 #include <cmath> |
| 9 | 8 |
| 10 #include "cc/base/rolling_time_delta_history.h" | 9 #include "cc/base/rolling_time_delta_history.h" |
| 11 | 10 |
| 12 namespace cc { | 11 namespace cc { |
| 13 | 12 |
| 14 RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size) | 13 RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size) |
| 15 : next_index_(0), max_size_(max_size) { | 14 : max_size_(max_size) {} |
| 16 sample_vector_.reserve(max_size_); | |
| 17 } | |
| 18 | 15 |
| 19 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {} | 16 RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {} |
| 20 | 17 |
| 21 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) { | 18 void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) { |
| 22 if (max_size_ == 0) | 19 if (max_size_ == 0) |
| 23 return; | 20 return; |
| 24 | 21 |
| 25 if (sample_vector_.size() == max_size_) { | 22 if (sample_set_.size() == max_size_) { |
| 26 sample_vector_[next_index_++] = time; | 23 sample_set_.erase(chronological_sample_deque_.front()); |
| 27 if (next_index_ == max_size_) | 24 chronological_sample_deque_.pop_front(); |
| 28 next_index_ = 0; | |
| 29 } else { | |
| 30 sample_vector_.push_back(time); | |
| 31 } | 25 } |
| 26 |
| 27 TimeDeltaMultiset::iterator it = sample_set_.insert(time); |
| 28 chronological_sample_deque_.push_back(it); |
| 32 } | 29 } |
| 33 | 30 |
| 34 void RollingTimeDeltaHistory::Clear() { | 31 void RollingTimeDeltaHistory::Clear() { |
| 35 sample_vector_.clear(); | 32 chronological_sample_deque_.clear(); |
| 36 next_index_ = 0; | 33 sample_set_.clear(); |
| 37 } | 34 } |
| 38 | 35 |
| 39 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const { | 36 base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const { |
| 40 if (sample_vector_.size() == 0) | 37 if (sample_set_.size() == 0) |
| 41 return base::TimeDelta(); | 38 return base::TimeDelta(); |
| 42 | 39 |
| 43 double fraction = percent / 100.0; | 40 double fraction = percent / 100.0; |
| 41 |
| 44 if (fraction <= 0.0) | 42 if (fraction <= 0.0) |
| 45 return *std::min_element(sample_vector_.begin(), sample_vector_.end()); | 43 return *(sample_set_.begin()); |
| 46 | 44 |
| 47 if (fraction >= 1.0) | 45 if (fraction >= 1.0) |
| 48 return *std::max_element(sample_vector_.begin(), sample_vector_.end()); | 46 return *(sample_set_.rbegin()); |
| 49 | 47 |
| 50 size_t num_smaller_samples = | 48 size_t num_smaller_samples = |
| 51 static_cast<size_t>(std::ceil(fraction * sample_vector_.size())) - 1; | 49 static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1; |
| 52 | 50 |
| 53 std::vector<base::TimeDelta> v(sample_vector_.begin(), sample_vector_.end()); | 51 if (num_smaller_samples > sample_set_.size() / 2) { |
| 54 std::nth_element(v.begin(), v.begin() + num_smaller_samples, v.end()); | 52 size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1; |
| 55 return v[num_smaller_samples]; | 53 TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin(); |
| 54 for (size_t i = 0; i < num_larger_samples; i++) |
| 55 it++; |
| 56 return *it; |
| 57 } |
| 58 |
| 59 TimeDeltaMultiset::const_iterator it = sample_set_.begin(); |
| 60 for (size_t i = 0; i < num_smaller_samples; i++) |
| 61 it++; |
| 62 return *it; |
| 56 } | 63 } |
| 57 | 64 |
| 58 } // namespace cc | 65 } // namespace cc |
| OLD | NEW |