FizzBuzz Optimisation | Modulus vs Decrement Operator Performance This is not the typical debate in FizzBuzz code about how to handle repeated if statement. I believe that it is a matter of personal preference.
I am here to ask about the choice of repeated mathematical operation.
Usually the solution to a FizzBuzz code is to use the modulus operator on every iteration. However, I'm concerned with its performance impact.
From my knowledge, modulus operator is tightly connected with division operator, and has a considerable performance overhead compared to simple increment and decrement operator.
In addition, I don't believe compiler has the ability to analyse and optimise the use of modulus operator in a loop.
Hence, I give the alternative solution that uses only simple decrement operator. The following code are written in C++.
I seek insight on whether my approach will provide performance benefit, or if it is actually a horrible idea that worsen performance.
And if there are other ways I can optimize a FizzBuzz code even further. (I think in a modified FizzBuzz puzzle with lots of cases to compare against, a lookup table might be preferable)

Typical FizzBuzz solution.
for (unsigned int i = 1; i &lt;= 1000000; ++i) {
    if (!(i % 15))
        std::cout &lt;&lt; &quot;FizzBuzz\n&quot;;
    else if (!(i % 3))
        std::cout &lt;&lt; &quot;Fizz\n&quot;;
    else if (!(i % 5))
        std::cout &lt;&lt; &quot;Buzz\n&quot;;
    else
        std::cout &lt;&lt; i &lt;&lt; '\n';
}


My optimized code.
static const unsigned int fa {3};
static const unsigned int fb {5};

unsigned int a {fa};
unsigned int b {fb};

for (unsigned int i = 1; i &lt;= 1000000; ++i) {
    --a, --b;
    if (!a &amp;&amp; !b) {
        a = fa;
        b = fb;
        std::cout &lt;&lt; &quot;FizzBuzz\n&quot;;
    } else if (!a) {
        a = fa;
        std::cout &lt;&lt; &quot;Fizz\n&quot;;
    } else if (!b) {
        b = fb;
        std::cout &lt;&lt; &quot;Buzz\n&quot;;
    } else {
        std::cout &lt;&lt; i &lt;&lt; '\n';
    }
}
```
Looks good - you're using only simple integer addition and subtraction.
Except for the division hidden here:
    std::cout &lt;&lt; i &lt;&lt; '\n';

Note that division by a constant isn't necessarily as expensive as you think: it's always possible for an optimising compiler to implement it in terms of multiplication and bitwise operations.  And a test for being an exact multiple is often simpler (think of the well-known algorithms for testing divisibility by 9 or 11 in decimal).
If you really want to drive up performance, consider storing the integer value as a string (any kind), and implement the ++ operation character by character.
You may also be interested in High throughput Fizz Buzz over on the Programming Puzzles site.