Monthly Archives: December 2012

Overhead of the modulo operator

While optimizing a number-crunching algorithm, I noticed (almost by accident) that a large amount of time was spent performing modulo/remainder operations (%) used for wrapping around the ends of arrays.  I found that changing i = (i + 1) % n; to i++; while(i >= n) { i -= n; } sped up my code.  […]