statistics_benchmark.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <cstddef>
  17. #include <random>
  18. #include <vector>
  19. #include <benchmark/benchmark.h>
  20. #include <audio_utils/Statistics.h>
  21. template <typename T>
  22. static void initUniform(std::vector<T> &data, T rangeMin, T rangeMax) {
  23. const size_t count = data.capacity();
  24. std::minstd_rand gen(count);
  25. std::uniform_real_distribution<T> dis(rangeMin, rangeMax);
  26. for (auto &datum : data) {
  27. datum = dis(gen);
  28. }
  29. }
  30. template <typename Stats>
  31. static void BM_MeanVariance(benchmark::State& state, int iterlimit, int alphalimit) {
  32. const float alpha = 1. - alphalimit * std::numeric_limits<float>::epsilon();
  33. Stats stat(alpha);
  34. using T = decltype(stat.getMin());
  35. constexpr size_t count = 1 << 20; // exactly one "mega" samples from the distribution.
  36. constexpr T range = 1.;
  37. std::vector<T> data(count);
  38. initUniform(data, -range, range);
  39. // Run the test
  40. int iters = 0;
  41. while (state.KeepRunning()) {
  42. benchmark::DoNotOptimize(data.data());
  43. for (const auto &datum : data) {
  44. stat.add(datum);
  45. }
  46. benchmark::ClobberMemory();
  47. if (++iters % iterlimit == 0) {
  48. printf("%d> alpha:%f mean:%.17g variance:%.17g\n",
  49. iters, alpha, (double)stat.getMean(), (double)stat.getPopVariance());
  50. stat.reset();
  51. }
  52. }
  53. state.SetComplexityN(count);
  54. }
  55. // Test case:
  56. // Do we work correctly within the capacity of float statistics when alpha == 1?
  57. //
  58. // 1 << 23 samples is the mantissa limited capacity of float statistics if alpha == 1.
  59. static constexpr int float_iterlimit = 8;
  60. // alphalimit of 0 means alpha exactly equals one.
  61. static constexpr int alpha_equals_one_alphalimit = 0;
  62. // benchmark running float
  63. static void BM_MeanVariance_float_float_float(benchmark::State &state) {
  64. BM_MeanVariance<android::audio_utils::Statistics<float, float, float>>(state,
  65. float_iterlimit, alpha_equals_one_alphalimit);
  66. }
  67. BENCHMARK(BM_MeanVariance_float_float_float);
  68. // benchmark reference float
  69. static void BM_RefMeanVariance_float_float(benchmark::State &state) {
  70. BM_MeanVariance<android::audio_utils::ReferenceStatistics<float, float>>(state,
  71. float_iterlimit, alpha_equals_one_alphalimit);
  72. }
  73. BENCHMARK(BM_RefMeanVariance_float_float);
  74. // benchmark running double
  75. static auto BM_MeanVariance_float_double_double(benchmark::State &state) {
  76. BM_MeanVariance<android::audio_utils::Statistics<float, double, double>>(state,
  77. float_iterlimit, alpha_equals_one_alphalimit);
  78. }
  79. BENCHMARK(BM_MeanVariance_float_double_double);
  80. // benchmark reference double
  81. static auto BM_RefMeanVariance_float_double(benchmark::State &state) {
  82. BM_MeanVariance<android::audio_utils::ReferenceStatistics<float, double>>(state,
  83. float_iterlimit, alpha_equals_one_alphalimit);
  84. }
  85. BENCHMARK(BM_RefMeanVariance_float_double);
  86. // benchmark running float + kahan
  87. static auto BM_MeanVariance_float_float_Kahan(benchmark::State &state) {
  88. BM_MeanVariance<android::audio_utils::Statistics<float, float,
  89. android::audio_utils::KahanSum<float>>>(state,
  90. float_iterlimit, alpha_equals_one_alphalimit);
  91. }
  92. BENCHMARK(BM_MeanVariance_float_float_Kahan);
  93. // benchmark running float + Neumaier
  94. static auto BM_MeanVariance_float_float_Neumaier(benchmark::State &state) {
  95. BM_MeanVariance<android::audio_utils::Statistics<float, float,
  96. android::audio_utils::NeumaierSum<float>>>(state,
  97. float_iterlimit, alpha_equals_one_alphalimit);
  98. }
  99. BENCHMARK(BM_MeanVariance_float_float_Neumaier);
  100. // Test case:
  101. // Do we work correctly for very large N statistics when alpha is 1 - 32 * epsilon?
  102. // This simulates long term statistics collection, where the alpha weighted windowing
  103. // permits us to exceed 1 << 23 samples reliably.
  104. //
  105. // 1 << 25 samples exceeds the mantissa limited capacity of float statistics if alpha == 1...
  106. static constexpr int float_overflow_iterlimit = 32;
  107. // but we use an alphalimit of 32, means 1. - (alphalimit * epsilon) approx = 0.999996.
  108. // This should allow statistics collection indefinitely.
  109. static constexpr int alpha_safe_upperbound_iterlimit = 32;
  110. // benchmark running float at alpha
  111. static auto BM_MeanVariance_float_float_float_alpha(benchmark::State &state) {
  112. BM_MeanVariance<android::audio_utils::Statistics<float, float, float>>(state,
  113. float_overflow_iterlimit, alpha_safe_upperbound_iterlimit);
  114. }
  115. BENCHMARK(BM_MeanVariance_float_float_float_alpha);
  116. // benchmark running double
  117. static auto BM_MeanVariance_float_double_double_alpha(benchmark::State &state) {
  118. BM_MeanVariance<android::audio_utils::Statistics<float, double, double>>(state,
  119. float_overflow_iterlimit, alpha_safe_upperbound_iterlimit);
  120. }
  121. BENCHMARK(BM_MeanVariance_float_double_double_alpha);
  122. BENCHMARK_MAIN();