Find the answer to your Linux question:
Results 1 to 6 of 6
Just curious about the -O, -O1, -O2, -O3 switches. Are there any pros and cons of using optimisation (or any of these switches in particular)? Geese....
  1. #1
    Linux User geese's Avatar
    Join Date
    Jan 2005
    Location
    Florida / Trinidad W.I.
    Posts
    284

    gcc optimisation -O, -O1, -O2, -O3?

    Just curious about the -O, -O1, -O2, -O3 switches.

    Are there any pros and cons of using optimisation (or any of these switches in particular)?


    Geese.
    Registered Linux user # 395739
    direplay.com

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I could summarize the authoritative answer, but that would waste your time and mine. Instead. go here.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux User geese's Avatar
    Join Date
    Jan 2005
    Location
    Florida / Trinidad W.I.
    Posts
    284
    "Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program"

    Okay, so basically optimisation can produce binaries with grater performance and smaller size with the trade off being longer compilation time.

    correct?
    Registered Linux user # 395739
    direplay.com

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    They can be greater performance (that is, smaller execution time), and sometimes smaller size, but sometimes the speed is purchased at the price of greater size.

    And in just about all cases, you pay greater compilation time, and sometimes significantly more memory required for compilation.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by wje_lf View Post
    They can be greater performance (that is, smaller execution time), and sometimes smaller size, but sometimes the speed is purchased at the price of greater size.

    And in just about all cases, you pay greater compilation time, and sometimes significantly more memory required for compilation.
    Bear also in mind that there's really no absolute answer to this.

    What a given "optimization" does is well defined on the man page for gcc. How a given "optimization" (note the usage of the quotation marks :P ) performs depends on many factors, that ranges from the program itself to the hardware you run it on, mainly the cpu, it's cache size, and the hard drive you run it from.

    -O3 is supposed to increase performance, but bigger code might not fit in the cpu cache, which in turns, virtually means a performance drop that is much bigger than any optimization that -O3 might provide.

    If the code is bigger, it takes more hard drive space, which, in turn, means that you will be wasting more time reading the file. This can be a serious problem on slow drives, or slow filesystems. Not to speak about the increased power consumption on laptops, which is a side effect.

    Also, there's the source code: some programs simply perform worse with -O3, regardless of how cool that optimization level might seem. And another problem is that -O3 is known to break compilations lots of times for lots of programs (Gentoo users do know this for sure, and a lot of ebuilds filter -O3).

    Most distros use -O2 as default, some people like -Os for laptops, which produces smaller code. I would only use -O3 on packages where it's known to be a good thing, and leave -O2 for the rest, which is much safer and usually performs better.

    If cpu level cache is a bottleneck, -Os can help.

    There's enough discussion about -O<whatever> on the net.

  6. #6
    Linux User geese's Avatar
    Join Date
    Jan 2005
    Location
    Florida / Trinidad W.I.
    Posts
    284

    Thumbs up

    Thanks a lot wje_lf and i92guboj.
    I have a better understanding of optimisation now.
    Registered Linux user # 395739
    direplay.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...