I am writing a digital filter and I need to save the latest X values โโand sum them all together.
Now there are two possible approaches. Or, I move the entire array with memmove
to free up space for the next value, and have the necessary indexes for the array as hardcoded values โโin my summing algorithm.
memmove(&Fifo[0], &Fifo[1], 12 * 4); // Shift array to the left Result += Factor[1] * (Fifo[5] + Fifo[7]); Result += Factor[2] * (Fifo[4] + Fifo[8]); Result += Factor[3] * (Fifo[3] + Fifo[9]); Result += Factor[4] * (Fifo[2] + Fifo[10]); Result += Factor[5] * (Fifo[1] + Fifo[11]); Result += Factor[6] * (Fifo[0] + Fifo[12]);
Or, on the contrary, I do not copy any memory, but instead increase the counter and calculate each index from this using the modulo operation (for example, a circular buffer).
i++; // Increment the index Result += Factor[1] * (Fifo[(i + 5) % 13] + Fifo[(i + 7) % 13]); Result += Factor[2] * (Fifo[(i + 4) % 13] + Fifo[(i + 8) % 13]); Result += Factor[3] * (Fifo[(i + 3) % 13] + Fifo[(i + 9) % 13]); Result += Factor[4] * (Fifo[(i + 2) % 13] + Fifo[(i + 10) % 13]); Result += Factor[5] * (Fifo[(i + 1) % 13] + Fifo[(i + 11) % 13]); Result += Factor[6] * (Fifo[(i + 0) % 13] + Fifo[(i + 12) % 13]);
With its integrated ARM processor, I was wondering what would be more efficient. Since I assume that the CPU must move at least one 32-bit value internally in order to perform a modulo operation, can it be that simply moving the entire array is as fast as calculating the correct indexes?
source share