3 ms·
The ring buffer is a nice trick, but it only works on array sizes that are a multiple of 2. The generalized version would be "array[i++ % ARRAY_SIZE] = data". I
by matthavener 15y ago
The ring buffer is a nice trick, but it only works on array sizes that are a multiple of 2. The generalized version would be "array[i++ % ARRAY_SIZE] = data". If ARRAY_SIZE happens to be a multiple of two, I'm sure most compilers would optimize to a bitmask anyways..
- deleted 15y ago[deleted]
- mansr 15y agoAnd if it is not a power of 2, the % operation will be anything but fast. If a size not a power of 2 is strictly required, a simple increment and compare is almost certainly faster.