Chromium Code Reviews| Index: media/base/moving_average.cc |
| diff --git a/media/base/moving_average.cc b/media/base/moving_average.cc |
| index b78f4c0e3dc90a13b00068e31c031a4e39739813..1a4e1bd758b8a9add84f9f646f6c9384d92a4fb7 100644 |
| --- a/media/base/moving_average.cc |
| +++ b/media/base/moving_average.cc |
| @@ -9,8 +9,7 @@ |
| namespace media { |
| MovingAverage::MovingAverage(size_t depth) |
| - : depth_(depth), count_(0), samples_(depth_) { |
| -} |
| + : depth_(depth), count_(0), samples_(depth_), square_sum_us_(0) {} |
| MovingAverage::~MovingAverage() { |
| } |
| @@ -20,6 +19,8 @@ void MovingAverage::AddSample(base::TimeDelta sample) { |
| // exceeds |depth_|. |
| base::TimeDelta& oldest = samples_[count_++ % depth_]; |
| total_ += sample - oldest; |
| + square_sum_us_ += sample.InMicroseconds() * sample.InMicroseconds() - |
| + oldest.InMicroseconds() * oldest.InMicroseconds(); |
| oldest = sample; |
| } |
| @@ -32,9 +33,23 @@ base::TimeDelta MovingAverage::Average() const { |
| return total_ / std::min(static_cast<uint64_t>(depth_), count_); |
| } |
| +base::TimeDelta MovingAverage::Deviation() const { |
| + DCHECK_GT(count_, 0u); |
| + |
| + const int size = std::min(static_cast<uint64_t>(depth_), count_); |
| + const int64_t average_us = Average().InMicroseconds(); |
| + const int64_t sqr_deviation_us = |
| + square_sum_us_ / size - average_us * average_us; |
|
DaleCurtis
2015/12/01 20:11:28
Cast upper or lower to double and store as double?
qiangchen
2015/12/01 22:37:07
Done.
Not sure are you fine with implicit cast. A
|
| + DCHECK_GE(sqr_deviation_us, 0); |
| + |
| + return base::TimeDelta::FromMicroseconds( |
| + static_cast<int64_t>(sqrt(sqr_deviation_us))); |
| +} |
| + |
| void MovingAverage::Reset() { |
| count_ = 0; |
| total_ = base::TimeDelta(); |
| + square_sum_us_ = 0; |
| std::fill(samples_.begin(), samples_.end(), base::TimeDelta()); |
| } |