2

I couldn't find an answer to this one. Assuming optimization does not break the code, why is -O3 or -O2 optimization not the default in C/C++? Why should we explicitly pass this flag? I cannot think of any situation where you would want your code to be not optimized.

I really don't understand why is this question closed and how it's opinion based.

merovingian
  • 515
  • 2
  • 9
  • 1
    I don't think "Why would you want a slower compilation time?" is the question you mean to ask. Or is this question about how long it takes to compile? – lucidbrot Oct 01 '22 at 09:22
  • 6
    You do not want to debug `-O3` code, that is not a pleasant experience. – Quimby Oct 01 '22 at 09:23
  • 1
    "_not the default in C/C++_": This has nothing to do with the programming language. It is a choice made by each specific compiler for itself. I guess you are talking about GCC or Clang here? – user17732522 Oct 01 '22 at 09:24
  • 1
    Yess gcc or clang. Maybe question is not specific to C/C++ but I had C++ in my mind @user17732522 – merovingian Oct 01 '22 at 09:25
  • You're right I'll fix it @lucidbrot – merovingian Oct 01 '22 at 09:27
  • Is this the only reason? @Quimby – merovingian Oct 01 '22 at 09:29
  • 2
    *"Assuming optimization does not break the code"* - many compilers set things like uninitialized pointers to specific values (that the runtime can check) but only in unoptimized builds. So in that sense optimization can "break" the code (as in making it harder to find the actual cause of a bug) – UnholySheep Oct 01 '22 at 09:30
  • @merovingian Not sure, my guess would be that the optimizations were not present in the first versions of the compilers hence it made sense to hide them behind different feature flags, same as warnings. – Quimby Oct 01 '22 at 09:45
  • 3
    In my job i almost always compile without optimisations. Three optimised build is only for making releases (which is usually the job of the integration server). If you're not invoking the compiler directly (which again you'd very rarely do) the default doesn't matter anyway – Alan Birtles Oct 01 '22 at 09:54
  • 1
    Depending on compiler, there's about a million possibilities - "no optimization", and the other 4999999 permutations. Note that for compilers like GCC `-O2` is merely a shorthand to enable/disable about 20 out of 100 different optimizations that can all be enabled/disabled individually. – Brendan Oct 01 '22 at 15:21
  • 1
    If you ask this question, I am tempted to think that you never use a debugger (otherwise you would have noticed how painful it is with optimized code). See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173). – prapin Oct 01 '22 at 18:00
  • *I really don't understand why is this question closed and how it's opinion based.* It is always opinion based to discuss about **default** compilation flags. What is factual however is that `-O0` is actually *very* useful for debugging. – prapin Oct 01 '22 at 18:42
  • Then the answer would be: '-O0' optimization is useful in blah. And 'O2' optimization is not used in blah blah. Therefore it is not the default. @prapin – merovingian Oct 02 '22 at 09:37
  • This is not an opinion-based question. I'm not asking about what song do you like – merovingian Oct 02 '22 at 09:38

1 Answers1

0

Increasing the level of optimization increases the compilation time because the compiler has to do more work to optimize your code. It also makes debugging more difficult.

Absolute1802
  • 53
  • 1
  • 8
  • On some code, having a low level of optimization (-O1) is actually faster than no optimization (-O0), because it does some cheap optimizations early and has less code to generate in the end. – Marc Glisse Oct 01 '22 at 11:14