primitives_benchmark.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2017 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/primitives.h>
  21. static void BM_MemcpyToFloatFromFloatWithClamping(benchmark::State& state) {
  22. const size_t count = state.range(0);
  23. const float srcMax = state.range(1);
  24. const float absMax = 1.413;
  25. std::vector<float> src(count);
  26. std::vector<float> dst(count);
  27. std::vector<float> expected(count);
  28. // Initialize src buffer with deterministic pseudo-random values
  29. std::minstd_rand gen(count);
  30. std::uniform_real_distribution<> dis(-srcMax, srcMax);
  31. for (size_t i = 0; i < count; i++) {
  32. src[i] = dis(gen);
  33. expected[i] = fmin(absMax, fmax(-absMax, src[i]));
  34. }
  35. // Run the test
  36. while (state.KeepRunning()) {
  37. benchmark::DoNotOptimize(src.data());
  38. benchmark::DoNotOptimize(dst.data());
  39. memcpy_to_float_from_float_with_clamping(dst.data(), src.data(), count, 1.413);
  40. benchmark::ClobberMemory();
  41. }
  42. if (expected != dst) {
  43. state.SkipWithError("Incorrect clamping!");
  44. }
  45. state.SetComplexityN(state.range(0));
  46. }
  47. BENCHMARK(BM_MemcpyToFloatFromFloatWithClamping)->RangeMultiplier(2)->Ranges({{10, 8<<12}, {1, 2}});
  48. static void BM_MemcpyFloat(benchmark::State& state) {
  49. const size_t count = state.range(0);
  50. std::vector<float> src(count);
  51. std::vector<float> dst(count);
  52. // Initialize src buffer with deterministic pseudo-random values
  53. std::minstd_rand gen(count);
  54. std::uniform_real_distribution<> dis;
  55. for (size_t i = 0; i < count; i++) {
  56. src[i] = dis(gen);
  57. }
  58. // Run the test
  59. while (state.KeepRunning()) {
  60. benchmark::DoNotOptimize(src.data());
  61. benchmark::DoNotOptimize(dst.data());
  62. memcpy(dst.data(), src.data(), count * sizeof(float));
  63. benchmark::ClobberMemory();
  64. }
  65. if (src != dst) {
  66. state.SkipWithError("Incorrect memcpy!");
  67. }
  68. state.SetComplexityN(state.range(0));
  69. }
  70. BENCHMARK(BM_MemcpyFloat)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
  71. static void BM_MemcpyToFloatFromI16(benchmark::State& state) {
  72. const size_t count = state.range(0);
  73. std::vector<int16_t> src(count);
  74. std::vector<float> dst(count);
  75. // Initialize src buffer with deterministic pseudo-random values
  76. std::minstd_rand gen(count);
  77. std::uniform_int_distribution<> dis(INT16_MIN, INT16_MAX);
  78. for (size_t i = 0; i < count; i++) {
  79. src[i] = dis(gen);
  80. }
  81. // Run the test
  82. while (state.KeepRunning()) {
  83. benchmark::DoNotOptimize(src.data());
  84. benchmark::DoNotOptimize(dst.data());
  85. memcpy_to_float_from_i16(dst.data(), src.data(), count);
  86. benchmark::ClobberMemory();
  87. }
  88. state.SetComplexityN(state.range(0));
  89. }
  90. BENCHMARK(BM_MemcpyToFloatFromI16)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
  91. static void BM_MemcpyToI16FromFloat(benchmark::State& state) {
  92. const size_t count = state.range(0);
  93. std::vector<float> src(count);
  94. std::vector<int16_t> dst(count);
  95. // Initialize src buffer with deterministic pseudo-random values
  96. std::minstd_rand gen(count);
  97. std::uniform_real_distribution<> dis;
  98. for (size_t i = 0; i < count; i++) {
  99. src[i] = dis(gen);
  100. }
  101. // Run the test
  102. while (state.KeepRunning()) {
  103. benchmark::DoNotOptimize(src.data());
  104. benchmark::DoNotOptimize(dst.data());
  105. memcpy_to_i16_from_float(dst.data(), src.data(), count);
  106. benchmark::ClobberMemory();
  107. }
  108. state.SetComplexityN(state.range(0));
  109. }
  110. BENCHMARK(BM_MemcpyToI16FromFloat)->RangeMultiplier(2)->Ranges({{10, 8<<12}});
  111. BENCHMARK_MAIN();