2

If I have some code with simple arithmetics that is repeating several times. Will the compiler automatically optimize it?

Here the example:

someArray[index + 1] = 5;
otherArray[index + 1] = 7;

Does it make sense to introduce variable nextIndex = index + 1 from the perfomance point of view, (not from the point of view of good readable and maintanable code) or the compiler will do such optimization automatically?

10
  • Well, if index is a fundamental integral type, most likely it will with optimizations enabled. Commented Aug 3, 2017 at 11:47
  • 4
    Why don't you build with optimization enabled, and check the generated code? Commented Aug 3, 2017 at 11:47
  • 1
    Compile it and check the assembly. Conceivably it could but I'm doubtful. Commented Aug 3, 2017 at 11:47
  • 1
    @specializt these are not the same object being indexed. Even if they were, a non-inlined overloaded operator [] could throw the optimizer off. Commented Aug 3, 2017 at 11:53
  • oh ... misread it. Commented Aug 3, 2017 at 11:54

3 Answers 3

2

You should not worry about trivial optimization like this because almost all compilers do it last 10-15 years or longer.

But if you have a really critical place in your code and want to get maximal speed of running, than you can check generated assembler code for this lines to be sure that compiler did this trivial optimization.

In some cases one more arithmetic addition could be more faster version of code than saving in register or memory, and compilers knows about this. You can make your code slower if you try optimize trivial cases manually.

And you can use online services like https://gcc.godbolt.org for check generated code (support gcc, clang, icc in several version).

Sign up to request clarification or add additional context in comments.

Comments

1

The old adage "suck it and see" seems to be appropriate here. We often forget that by far the most common processors are 4/8/16 bit micros with weird and wonderful application specific architectures and suitably odd vendor specific compilers to go with them. They frequently have compiler extensions to "aid" (or confuse) the compiler into producing "better" code.

One DSP from early 2000s carried out 8 instructions per clock-cycle in parallel in a pipeline (complex - "load+increment+multiply+add+round"). The precondition for this to work was that everything had to be preloaded into the registers beforehand. This meant that registers were obviously at a premium (as always). With this architecture it was frequently better to bin results to free registers and use free slots that couldn't be paralleled (some instructions precluded the use of others in the same cycle) to recalculate it later. Did the compiler get this "right"?. Yes, it often kept the result to reuse later with the result that it stalled the pipeline due to lack of registers which resulted in slower execution speed.

So, you compiled it, examined it, profiled it etc. to make sure that the when when the compiler got it "right" we could go in and fix it. Without additional semantic information which is not supported by the language it is really hard to know what "right" is.

Conclusion: Suck it and see

Comments

0

Yes. It's a common optimization. https://en.wikipedia.org/wiki/Common_subexpression_elimination

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.