1 |
/* |
2 |
Square wave generator benchmark |
3 |
|
4 |
This is a benchmark for comparison between an integer math solution and a |
5 |
specialized pulse solution. |
6 |
|
7 |
Copyright (C) 2019 Christian Schoenebeck <cuse@users.sf.net> |
8 |
*/ |
9 |
|
10 |
#include "lfobench.h" |
11 |
|
12 |
#include "../src/engines/common/LFOSquareIntMath.h" |
13 |
#include "../src/engines/common/LFOSquarePulse.h" |
14 |
|
15 |
// return value of this benchmark |
16 |
// to indicate the best performing solution |
17 |
#define SQUARE_INT_MATH_SOLUTION 60 |
18 |
#define SQUARE_PULSE_SOLUTION 61 |
19 |
#define INVALID_RESULT -1 |
20 |
|
21 |
#if SIGNED |
22 |
LFOSquareIntMath<LFO::range_signed>* pSquareIntLFO = NULL; |
23 |
LFOSquarePulse<LFO::range_signed>* pSquarePulse = NULL; |
24 |
#else // unsigned |
25 |
LFOSquareIntMath<LFO::range_unsigned>* pSquareIntLFO = NULL; |
26 |
LFOSquarePulse<LFO::range_unsigned>* pSquarePulse = NULL; |
27 |
#endif |
28 |
|
29 |
double square_int_math(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) { |
30 |
// pro forma |
31 |
pSquareIntLFO->trigger(frequency, LFO::start_level_min, 0 /* max. internal depth */, 1200, false, (unsigned int) SAMPLING_RATE); |
32 |
//pSquareIntLFO->setPhase(180); |
33 |
//pSquareIntLFO->setFrequency(frequency*2, SAMPLING_RATE); |
34 |
|
35 |
clock_t stop_time; |
36 |
clock_t start_time = clock(); |
37 |
|
38 |
for (int run = 0; run < RUNS; run++) { |
39 |
pSquareIntLFO->updateByMIDICtrlValue(127); // pro forma |
40 |
for (int i = 0; i < steps; ++i) { |
41 |
//pSquareIntLFO->updateByMIDICtrlValue(float(i)/float(steps)*127.f); |
42 |
pDestinationBuffer[i] = pSquareIntLFO->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load |
43 |
} |
44 |
} |
45 |
|
46 |
stop_time = clock(); |
47 |
double elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0); |
48 |
#if ! SILENT |
49 |
printf("int math solution elapsed time: %.1f ms\n", elapsed_time); |
50 |
#endif |
51 |
|
52 |
return elapsed_time; |
53 |
} |
54 |
|
55 |
double square_pulse(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) { |
56 |
// pro forma |
57 |
pSquarePulse->trigger(frequency, LFO::start_level_min, 0 /* max. internal depth */, 1200, false, (unsigned int) SAMPLING_RATE); |
58 |
//pSquarePulse->setPhase(180); |
59 |
//pSquarePulse->setFrequency(frequency*2, SAMPLING_RATE); |
60 |
|
61 |
clock_t stop_time; |
62 |
clock_t start_time = clock(); |
63 |
|
64 |
for (int run = 0; run < RUNS; run++) { |
65 |
pSquarePulse->updateByMIDICtrlValue(127); // pro forma |
66 |
for (int i = 0; i < steps; ++i) { |
67 |
//pSquarePulse->updateByMIDICtrlValue(float(i)/float(steps)*127.f); |
68 |
pDestinationBuffer[i] = pSquarePulse->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load |
69 |
} |
70 |
} |
71 |
|
72 |
stop_time = clock(); |
73 |
double elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0); |
74 |
#if ! SILENT |
75 |
printf("Pulse solution elapsed time: %.1f ms\n", elapsed_time); |
76 |
#endif |
77 |
|
78 |
return elapsed_time; |
79 |
} |
80 |
|
81 |
int main() { |
82 |
const int steps = STEPS; |
83 |
const int sinusoidFrequency = 100; // Hz |
84 |
|
85 |
#if ! SILENT |
86 |
printf("\n"); |
87 |
# if SIGNED |
88 |
printf("Signed square wave benchmark\n"); |
89 |
# else |
90 |
printf("Unsigned square wave benchmark\n"); |
91 |
# endif |
92 |
printf("----------------------------------\n"); |
93 |
printf("\n"); |
94 |
#endif |
95 |
|
96 |
#if SIGNED |
97 |
pSquareIntLFO = new LFOSquareIntMath<LFO::range_signed>(MAX); |
98 |
pSquarePulse = new LFOSquarePulse<LFO::range_signed>(MAX); |
99 |
#else // unsigned |
100 |
pSquareIntLFO = new LFOSquareIntMath<LFO::range_unsigned>(MAX); |
101 |
pSquarePulse = new LFOSquarePulse<LFO::range_unsigned>(MAX); |
102 |
#endif |
103 |
|
104 |
// output buffer for the calculated sinusoid wave |
105 |
smpl_t* pOutputBuffer = new smpl_t[steps]; |
106 |
// just an arbitrary amplitude envelope to simulate a bit higher memory bandwidth |
107 |
float* pAmplitude = new float[steps]; |
108 |
|
109 |
// pro forma - an arbitary amplitude envelope |
110 |
for (int i = 0; i < steps; ++i) |
111 |
pAmplitude[i] = (float) i / (float) steps; |
112 |
|
113 |
// going to store how long each solution took (in seconds) |
114 |
std::vector<BenchRes> results; |
115 |
|
116 |
|
117 |
|
118 |
results.push_back({ |
119 |
.algorithmID = SQUARE_INT_MATH_SOLUTION, |
120 |
.algorithmName = "int math", |
121 |
.timeMSecs = square_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency) |
122 |
}); |
123 |
#if OUTPUT_AS_RAW_WAVE |
124 |
output_as_raw_file("square_int_math.raw", pOutputBuffer, steps); |
125 |
#endif |
126 |
|
127 |
|
128 |
results.push_back({ |
129 |
.algorithmID = SQUARE_PULSE_SOLUTION, |
130 |
.algorithmName = "Pulse", |
131 |
.timeMSecs = square_pulse(pOutputBuffer, pAmplitude, steps, sinusoidFrequency) |
132 |
}); |
133 |
#if OUTPUT_AS_RAW_WAVE |
134 |
output_as_raw_file("square_pulse.raw", pOutputBuffer, steps); |
135 |
#endif |
136 |
|
137 |
|
138 |
#if ! SILENT |
139 |
printf("\nOK, 2nd try\n\n"); |
140 |
#endif |
141 |
|
142 |
|
143 |
results[0].timeMSecs += square_int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency); |
144 |
results[1].timeMSecs += square_pulse(pOutputBuffer, pAmplitude, steps, sinusoidFrequency); |
145 |
|
146 |
|
147 |
if (pAmplitude) delete[] pAmplitude; |
148 |
if (pOutputBuffer) delete[] pOutputBuffer; |
149 |
|
150 |
if (pSquareIntLFO) delete pSquareIntLFO; |
151 |
if (pSquarePulse) delete pSquarePulse; |
152 |
|
153 |
sortResultsFirstToBeBest(results); |
154 |
printResultSummary(results); |
155 |
|
156 |
return results[0].algorithmID; // return the winner's numeric algorithm ID |
157 |
} |