performance_timer.cc (2508B)
1 /* 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "modules/audio_processing/test/performance_timer.h" 12 13 #include <cmath> 14 #include <cstddef> 15 #include <cstdint> 16 #include <numeric> 17 18 #include "rtc_base/checks.h" 19 #include "system_wrappers/include/clock.h" 20 21 namespace webrtc { 22 namespace test { 23 24 PerformanceTimer::PerformanceTimer(int num_frames_to_process) 25 : clock_(Clock::GetRealTimeClock()) { 26 timestamps_us_.reserve(num_frames_to_process); 27 } 28 29 PerformanceTimer::~PerformanceTimer() = default; 30 31 void PerformanceTimer::StartTimer() { 32 start_timestamp_us_ = clock_->TimeInMicroseconds(); 33 } 34 35 void PerformanceTimer::StopTimer() { 36 RTC_DCHECK(start_timestamp_us_); 37 timestamps_us_.push_back(clock_->TimeInMicroseconds() - *start_timestamp_us_); 38 } 39 40 double PerformanceTimer::GetDurationAverage() const { 41 return GetDurationAverage(0); 42 } 43 44 double PerformanceTimer::GetDurationStandardDeviation() const { 45 return GetDurationStandardDeviation(0); 46 } 47 48 double PerformanceTimer::GetDurationAverage( 49 size_t number_of_warmup_samples) const { 50 RTC_DCHECK_GT(timestamps_us_.size(), number_of_warmup_samples); 51 const size_t number_of_samples = 52 timestamps_us_.size() - number_of_warmup_samples; 53 return static_cast<double>( 54 std::accumulate(timestamps_us_.begin() + number_of_warmup_samples, 55 timestamps_us_.end(), static_cast<int64_t>(0))) / 56 number_of_samples; 57 } 58 59 double PerformanceTimer::GetDurationStandardDeviation( 60 size_t number_of_warmup_samples) const { 61 RTC_DCHECK_GT(timestamps_us_.size(), number_of_warmup_samples); 62 const size_t number_of_samples = 63 timestamps_us_.size() - number_of_warmup_samples; 64 RTC_DCHECK_GT(number_of_samples, 0); 65 double average_duration = GetDurationAverage(number_of_warmup_samples); 66 67 double variance = std::accumulate( 68 timestamps_us_.begin() + number_of_warmup_samples, timestamps_us_.end(), 69 0.0, [average_duration](const double& a, const int64_t& b) { 70 return a + (b - average_duration) * (b - average_duration); 71 }); 72 73 return sqrt(variance / number_of_samples); 74 } 75 76 } // namespace test 77 } // namespace webrtc