Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "media/base/moving_average.h" | 5 #include "media/base/moving_average.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 namespace media { | 9 namespace media { |
| 10 | 10 |
| 11 MovingAverage::MovingAverage(size_t depth) | 11 MovingAverage::MovingAverage(size_t depth) |
| 12 : depth_(depth), count_(0), samples_(depth_) { | 12 : depth_(depth), count_(0), samples_(depth_), square_sum_us_(0) {} |
| 13 } | |
| 14 | 13 |
| 15 MovingAverage::~MovingAverage() { | 14 MovingAverage::~MovingAverage() { |
| 16 } | 15 } |
| 17 | 16 |
| 18 void MovingAverage::AddSample(base::TimeDelta sample) { | 17 void MovingAverage::AddSample(base::TimeDelta sample) { |
| 19 // |samples_| is zero-initialized, so |oldest| is also zero before |count_| | 18 // |samples_| is zero-initialized, so |oldest| is also zero before |count_| |
| 20 // exceeds |depth_|. | 19 // exceeds |depth_|. |
| 21 base::TimeDelta& oldest = samples_[count_++ % depth_]; | 20 base::TimeDelta& oldest = samples_[count_++ % depth_]; |
| 22 total_ += sample - oldest; | 21 total_ += sample - oldest; |
| 22 square_sum_us_ += sample.InMicroseconds() * sample.InMicroseconds() - | |
| 23 oldest.InMicroseconds() * oldest.InMicroseconds(); | |
| 23 oldest = sample; | 24 oldest = sample; |
| 24 } | 25 } |
| 25 | 26 |
| 26 base::TimeDelta MovingAverage::Average() const { | 27 base::TimeDelta MovingAverage::Average() const { |
| 27 DCHECK_GT(count_, 0u); | 28 DCHECK_GT(count_, 0u); |
| 28 | 29 |
| 29 // TODO(dalecurtis): Consider limiting |depth| to powers of two so that we can | 30 // TODO(dalecurtis): Consider limiting |depth| to powers of two so that we can |
| 30 // replace the integer divide with a bit shift operation. | 31 // replace the integer divide with a bit shift operation. |
| 31 | 32 |
| 32 return total_ / std::min(static_cast<uint64_t>(depth_), count_); | 33 return total_ / std::min(static_cast<uint64_t>(depth_), count_); |
| 33 } | 34 } |
| 34 | 35 |
| 36 base::TimeDelta MovingAverage::Deviation() const { | |
| 37 DCHECK_GT(count_, 0u); | |
| 38 | |
| 39 const double size = std::min(static_cast<uint64_t>(depth_), count_); | |
| 40 const double total_us = total_.InMicroseconds(); | |
| 41 double sqr_deviation_us = | |
| 42 (square_sum_us_ - total_us / size * total_us) / size; | |
|
DaleCurtis
2015/12/01 22:42:19
I think your old code w/ size as a double is bette
qiangchen
2015/12/01 23:35:33
Done.
| |
| 43 if (sqr_deviation_us < 0) | |
| 44 sqr_deviation_us = 0; | |
| 45 | |
| 46 return base::TimeDelta::FromMicroseconds(sqrt(sqr_deviation_us)); | |
| 47 } | |
| 48 | |
| 35 void MovingAverage::Reset() { | 49 void MovingAverage::Reset() { |
| 36 count_ = 0; | 50 count_ = 0; |
| 37 total_ = base::TimeDelta(); | 51 total_ = base::TimeDelta(); |
| 52 square_sum_us_ = 0; | |
| 38 std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); | 53 std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); |
| 39 } | 54 } |
| 40 | 55 |
| 41 } // namespace media | 56 } // namespace media |
| OLD | NEW |