I tested out a few different compiler optimizations on GNU GCC, in the CodeBlocks IDE. With each different optimization I ran the same test: I let my program try to build as big of a tree as it could in 1 second. The nodes of the tree were objects of a class I created.


  • [-O1]: 78,235 nodes generated.
  • [-O2]: 78,235 nodes generated.
  • [-O3]: 78,235 nodes generated.
  • Optimize more (for speed) [-O1]: 773,019 nodes generated.
  • Optimize even more (for speed) [-O2]: 773,019 nodes generated.
  • Optimize fully (for speed) [-O3]: 222,072 nodes generated.

So this is confusing. Standard O1, O2, O3 all perform exactly the same. I made sure to recompile each time I adjusted the compiler settings, so I don't think there's any error on my part.

The extra optimizations for O1 and O2 also perform exactly the same, but they perform significantly better than the O3 extra optimization (the optimization I assumed was for max speed).

Could anyone explain this?

EDIt - The exact same number of nodes generated by certain optimizations is actually to be expected, I just remembered this. The program performs iterative-deepening growth of the tree, so the number of nodes grows in huge chunks at a time.

  • What's the difference between the round of tests that did 78K nodes and the round that did 773K nodes? – Blrfl Dec 31 '18 at 4:17
  • @Blrfl There's an option CodeBlocks has for optimizing the GNU GCC compiler. You can just pick [-O2], which resulted in one of the 78K node tests, but there also a "Optimize even more (for speed) [-O2]" option. That resulted in the 773K test. – Inertial Ignorance Dec 31 '18 at 4:21
  • 2
    How did you benchmark the performance? The measurements might have significant jitter. Consider writing a script that records the performance of at least 40 runs at each configuration, and then look at aggregate statistics (histograms, mean performance, median performance, inter-quartile range and so on). Also given that your program grows in discrete chunks, it might be better to measure the time until a given number of chunks has been computed. That might make it easier to see differences that are here hidden when the program aborts just before completing the next chunk. – amon Dec 31 '18 at 10:38
  • 1
    You may want to edit your question to reflect that; as posted, the results are unclear. Plus everything amon said above. – Blrfl Dec 31 '18 at 12:42
  • @amon Good idea I'll run the tests that way and see what happens. – Inertial Ignorance Jan 1 at 6:33

Your Answer

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

Browse other questions tagged or ask your own question.