3 ms·
The point here is that with the Lua code the CPU is doing some extra work because mod is 0 based because of mathematics and Lua is 1 based by design. However m
by pmontra 3y ago
The point here is that with the Lua code the CPU is doing some extra work because mod is 0 based because of mathematics and Lua is 1 based by design.
However mod itself is doing extra work compared to incrementing a counter by one.
This is pseudocode because I don't use Lua much and I don't have the time to check the syntax now, but it should give the general idea
class CircBuf(size)
buf = new Array(size)
i = 1
add(value)
buf[i] = value
if i == size
i = 1
else
i = i + 1
The relative efficiency boils down to a comparison+increment vs mod on the particular CPU the code is running on. Mod should be a division, unless size is a power of two.