Question 1: execution time of calling the function

B will not have any effect. inline only suppresses the one-definition rule; it does not force the compiler to inline the function.

C is unlikely to have any impact; if the compiler determines that the function is a good candidate to be inlined, it will do so. Manually inlining it could make performance worse.

The other three options (A, D, and E) may all perform better or worse than each other depending on many factors. The biggest factor in all of this is the compiler. Modern compilers are very good at optimization. A, D, and E could all be trivially transformed into each other. Therefore, they might all be just as fast as each other.

The answer is therefore highly dependent on the specific compiler (and version of that compiler) as well as the compilation flags being used. Given a specific compiler, I would need to properly benchmark each option with optimizations turned all the way up in order to determine the correct answer.

If I were taking this test, I would refuse to answer this question and send a note to the proctor/author indicating that the question is defective.


Now that I have that out of the way, if we assume all compiler optimizations are disabled, D is likely to be the fastest simply because it is branchless. A and E both involve branching, and a failed branch prediction is costly.

I would expect D to be the fastest. A and E should perform about the same.


In my tests on gcc with -O3, E is optimized to a lookup table (like D) but A remains a series of conditional jumps. So in this particular test, D and E are both the correct answer.

Switching to clang with -O3, it optimizes both A and E to use a lookup table (like D). It generates equivalent assembly for all options.

Leave a Comment