0
\$\begingroup\$

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 <= 1000000; ++i) {
    if (!(i % 15))
        std::cout << "FizzBuzz\n";
    else if (!(i % 3))
        std::cout << "Fizz\n";
    else if (!(i % 5))
        std::cout << "Buzz\n";
    else
        std::cout << i << '\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 <= 1000000; ++i) {
    --a, --b;
    if (!a && !b) {
        a = fa;
        b = fb;
        std::cout << "FizzBuzz\n";
    } else if (!a) {
        a = fa;
        std::cout << "Fizz\n";
    } else if (!b) {
        b = fb;
        std::cout << "Buzz\n";
    } else {
        std::cout << i << '\n';
    }
}
```
\$\endgroup\$
4
  • 3
    \$\begingroup\$ "I seek insight on whether my approach will provide performance benefit, or if it is actually a horrible idea that worsen performance." You have two horses. Race them. Do you know what a profiler is? \$\endgroup\$ – Mast Jan 16 at 11:35
  • \$\begingroup\$ @Mast No. What's a profiler? \$\endgroup\$ – Desmond Rhodes Jan 16 at 11:37
  • \$\begingroup\$ Analysis of a program to find time/space complexity, frequency and duration of function calls and execution time. Wiki \$\endgroup\$ – Mast Jan 16 at 11:40
  • 1
    \$\begingroup\$ Fully agree with Mast, instead of speculating which is faster, test it! Write both pieces of code run then and measure the time it takes. Optimizing for performance without measuring is like painting your house blind. \$\endgroup\$ – Emily L. Jan 16 at 13:16
1
\$\begingroup\$

Looks good - you're using only simple integer addition and subtraction.

Except for the division hidden here:

    std::cout << i << '\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.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, the linked thread is crazy, and full of resources to read over. \$\endgroup\$ – Desmond Rhodes Jan 16 at 11:01
  • \$\begingroup\$ Can you please elaborate on how printing 'i' contain a hidden division? or link me to a resource to read in? Thanks in advance. \$\endgroup\$ – a.Li Jan 17 at 4:25
  • 1
    \$\begingroup\$ @a.Li - I'll turn that around into a challenge for you - convert an integer to a decimal string without library functions. Now do it without any / or %. It's not impossible, but not obvious either. \$\endgroup\$ – Toby Speight Jan 17 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.