2

I've seen a lot of people use -O2 when compiling their C++ code, but why would you not use -O3 instead? My assumption is that it makes the code more optimised/faster compared to using -O2 unless there's some caveats that I'm missing here, but I'm not sure the reasoning behind why using -O3 isn't the standard or why it's not widely used for compiling with g++ (at least from my observations).

BadUsernameIdea
  • 182
  • 1
  • 12
  • I guess O3 takes more time to compile so before production it could save engineering time to use O2 and just make it O3 after production. Better engineering run-time before launch + better app run-time after launch. – huseyin tugrul buyukisik Apr 04 '22 at 15:23
  • 3
    Historically, `-O3` had a bad reputation for sometimes slowing things down because of bloated code. It hasn't enabled loop unrolling for years without `-fprofile-use` PGO, but does still enable auto-vectorization (but that's part of -O2 in GCC12 and later) – Peter Cordes Apr 04 '22 at 23:04
  • @JérômeRichard - Kind of a duplicate, but the 9 year old answers there were already outdated at the time in recommending against `-O3`. It could be time for a fresh canonical. (Basile's answer there is useful, showing that as early as GCC4.8 major steps had been made to make `-O3` less likely to be slower.) – Peter Cordes Apr 05 '22 at 02:47
  • @PeterCordes I partially agree. I think it is better to update old post that are widely seen rather than creating duplicates. If the alternative post would not be famous, then I would fully agree. (I did not expect the bot to close the question based on my unique vote though). – Jérôme Richard Apr 05 '22 at 17:56
  • @PeterCordes Btw, this is not so rare auto-vectorization make the code slower because the compiler wrongly assume that the loop is hot (though not usual either). Having auto-vectorization enabled in `-O2` will certainly cause some code to be slower because of that. I guess they did that because many people used `-O2` for no real reasons resulting in many code not being vectorized... But the real issue was not to use `-O3` or at least `-ftree-vectorize`. Do you have any information about this choice? – Jérôme Richard Apr 05 '22 at 17:58
  • 1
    @JérômeRichard: No, I just know it changed; I haven't looked for discussion that led up to it. Agreed that it may hurt front-end throughput sometimes, especially with `char` arrays with massively bloated 31 iterations of cleanup fully peeled like GCC likes to do, unless they fixed that recently to keep the cleanup rolled up. – Peter Cordes Apr 05 '22 at 22:50

0 Answers0