mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
Add samples sum calculation
Bug: b/261160916, webrtc:14852 Change-Id: I88e464fce4673dd9b9683219b8d2837206579ba5 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/293942 Reviewed-by: Per Kjellander <perkj@webrtc.org> Commit-Queue: Sergey Silkin <ssilkin@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39386}
This commit is contained in:
parent
cec856991b
commit
8566e779e3
4 changed files with 18 additions and 0 deletions
|
@ -65,6 +65,12 @@ class SamplesStatsCounter {
|
|||
RTC_DCHECK(!IsEmpty());
|
||||
return *stats_.GetMax();
|
||||
}
|
||||
// Returns sum in O(1) time. This function may not be called if there are
|
||||
// no samples.
|
||||
double GetSum() const {
|
||||
RTC_DCHECK(!IsEmpty());
|
||||
return *stats_.GetSum();
|
||||
}
|
||||
// Returns average in O(1) time. This function may not be called if there are
|
||||
// no samples.
|
||||
double GetAverage() const {
|
||||
|
|
|
@ -61,6 +61,7 @@ TEST(SamplesStatsCounterTest, FullSimpleTest) {
|
|||
EXPECT_TRUE(!stats.IsEmpty());
|
||||
EXPECT_DOUBLE_EQ(stats.GetMin(), 1.0);
|
||||
EXPECT_DOUBLE_EQ(stats.GetMax(), 100.0);
|
||||
EXPECT_DOUBLE_EQ(stats.GetSum(), 5050.0);
|
||||
EXPECT_NEAR(stats.GetAverage(), 50.5, 1e-6);
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
double p = i / 100.0;
|
||||
|
|
|
@ -51,6 +51,7 @@ class RunningStatistics {
|
|||
void AddSample(T sample) {
|
||||
max_ = std::max(max_, sample);
|
||||
min_ = std::min(min_, sample);
|
||||
sum_ += sample;
|
||||
++size_;
|
||||
// Welford's incremental update.
|
||||
const double delta = sample - mean_;
|
||||
|
@ -123,6 +124,14 @@ class RunningStatistics {
|
|||
return max_;
|
||||
}
|
||||
|
||||
// Returns sum in O(1) time.
|
||||
absl::optional<double> GetSum() const {
|
||||
if (size_ == 0) {
|
||||
return absl::nullopt;
|
||||
}
|
||||
return sum_;
|
||||
}
|
||||
|
||||
// Returns mean in O(1) time.
|
||||
absl::optional<double> GetMean() const {
|
||||
if (size_ == 0) {
|
||||
|
@ -153,6 +162,7 @@ class RunningStatistics {
|
|||
T max_ = minus_infinity_or_min<T>();
|
||||
double mean_ = 0;
|
||||
double cumul_ = 0; // Variance * size_, sometimes noted m2.
|
||||
double sum_ = 0;
|
||||
};
|
||||
|
||||
} // namespace webrtc_impl
|
||||
|
|
|
@ -61,6 +61,7 @@ TEST(RunningStatistics, FullSimpleTest) {
|
|||
|
||||
EXPECT_DOUBLE_EQ(*stats.GetMin(), 1.0);
|
||||
EXPECT_DOUBLE_EQ(*stats.GetMax(), 100.0);
|
||||
EXPECT_DOUBLE_EQ(*stats.GetSum(), 5050.0);
|
||||
// EXPECT_DOUBLE_EQ is too strict (max 4 ULP) for this one.
|
||||
ASSERT_NEAR(*stats.GetMean(), 50.5, 1e-10);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue