analyzer_benchmark.cpp (3044B)
1 2 #include <benchmark/benchmark.h> 3 4 #include "dsp/analyzer.hpp" 5 6 using namespace bogaudio::dsp; 7 8 static void BM_Analyzer_HanningWindowInit(benchmark::State& state) { 9 for (auto _ : state) { 10 delete new HanningWindow(1024); 11 } 12 } 13 BENCHMARK(BM_Analyzer_HanningWindowInit); 14 15 static void BM_Analyzer_HanningWindowApply(benchmark::State& state) { 16 const int n = 1024; 17 HanningWindow w(n); 18 float in[n]; 19 std::fill_n(in, n, 1.1); 20 float out[n] {}; 21 for (auto _ : state) { 22 w.apply(in, out); 23 in[0] = out[0]; // prevents the call from being optimized away? 24 } 25 } 26 BENCHMARK(BM_Analyzer_HanningWindowApply); 27 28 static void BM_Analyzer_RuntimeFFT1024(benchmark::State& state) { 29 const int n = 1024; 30 ffft::FFTReal<float> fft(n); 31 float in[n]; 32 std::fill_n(in, n, 1.1); 33 float out[n] {}; 34 for (auto _ : state) { 35 fft.do_fft(out, in); 36 } 37 } 38 BENCHMARK(BM_Analyzer_RuntimeFFT1024); 39 40 static void BM_Analyzer_CompileTimeFFT1024(benchmark::State& state) { 41 FFT1024 fft; 42 const int n = 1024; 43 float in[n]; 44 std::fill_n(in, n, 1.1); 45 float out[n] {}; 46 for (auto _ : state) { 47 fft.do_fft(out, in); 48 } 49 } 50 BENCHMARK(BM_Analyzer_CompileTimeFFT1024); 51 52 static void BM_Analyzer_RuntimeFFT4096(benchmark::State& state) { 53 const int n = 4096; 54 ffft::FFTReal<float> fft(n); 55 float in[n]; 56 std::fill_n(in, n, 1.1); 57 float out[n] {}; 58 for (auto _ : state) { 59 fft.do_fft(out, in); 60 } 61 } 62 BENCHMARK(BM_Analyzer_RuntimeFFT4096); 63 64 static void BM_Analyzer_CompileTimeFFT4096(benchmark::State& state) { 65 FFT4096 fft; 66 const int n = 4096; 67 float in[n]; 68 std::fill_n(in, n, 1.1); 69 float out[n] {}; 70 for (auto _ : state) { 71 fft.do_fft(out, in); 72 } 73 } 74 BENCHMARK(BM_Analyzer_CompileTimeFFT4096); 75 76 static void BM_Analyzer_SpectrumAnalyzerStep(benchmark::State& state) { 77 SpectrumAnalyzer sa( 78 SpectrumAnalyzer::SIZE_1024, 79 SpectrumAnalyzer::OVERLAP_1, 80 SpectrumAnalyzer::WINDOW_HANNING, 81 44100.0 82 ); 83 float in[8] = { 0.0, 0.7, 1.0, 0.7, 0.0, -0.7, -1.0, -0.7 }; 84 int i = 0; 85 for (auto _ : state) { 86 sa.step(in[i]); 87 ++i; 88 i %= 8; 89 } 90 } 91 BENCHMARK(BM_Analyzer_SpectrumAnalyzerStep); 92 93 // static void BM_Analyzer_SpectrumAnalyzerProcess(benchmark::State& state) { 94 // SpectrumAnalyzer sa( 95 // SpectrumAnalyzer::SIZE_1024, 96 // SpectrumAnalyzer::OVERLAP_1, 97 // SpectrumAnalyzer::WINDOW_HANNING, 98 // 44100.0 99 // ); 100 // float in[8] = { 0.0, 0.7, 1.0, 0.7, 0.0, -0.7, -1.0, -0.7 }; 101 // for (int i = 0; i < 1024; ++i) { 102 // sa.step(in[i % 8]); 103 // } 104 // 105 // for (auto _ : state) { 106 // sa.process(sa._samples); 107 // } 108 // } 109 // BENCHMARK(BM_Analyzer_SpectrumAnalyzerProcess); 110 111 static void BM_Analyzer_SpectrumAnalyzerGetMagnitudes(benchmark::State& state) { 112 SpectrumAnalyzer sa( 113 SpectrumAnalyzer::SIZE_1024, 114 SpectrumAnalyzer::OVERLAP_1, 115 SpectrumAnalyzer::WINDOW_HANNING, 116 44100.0 117 ); 118 const int nBins = 256; 119 float bins[nBins]; 120 float in[8] = { 0.0, 0.7, 1.0, 0.7, 0.0, -0.7, -1.0, -0.7 }; 121 for (int i = 0; i < 1024; ++i) { 122 sa.step(in[i % 8]); 123 } 124 125 for (auto _ : state) { 126 sa.getMagnitudes(bins, nBins); 127 } 128 } 129 BENCHMARK(BM_Analyzer_SpectrumAnalyzerGetMagnitudes);