gcc (or g++ for C++ code) has varying optimization levels. Specific types of code or target platforms (such as embedded) might benefit from an alternate gcc flag. But for most situations, you can use –O0 for debug build and –O2 for release build. The default is –O0. An old friend (who is an Economics specialist) ran into this because he wanted to write a fairly simple C++ application for Economics data simulation, that would be cross-platform.
His XCode build ran noticeably faster (such as 2 sec vs. 54 sec) than when he used g++ directly on Mac OS X, or g++ in cygwin on Windows, or g++ on Linux. So I took his initial code and got it to build using Qt Creator 2.0.0 on Windows 7. I noticed the g++ optimization flags when looking at Qt Creator’s make files (which use qmake and gcc / g++) (notice the -O2 part) (this is from Makefile.Release):
CC = gcc
CXX = g++
DEFINES = -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_THREAD_SUPPORT
CFLAGS = -O2 -Wall $(DEFINES)
CXXFLAGS = -O2 -frtti -fexceptions -mthreads -Wall $(DEFINES)
I’m sure I used gcc directly like this at some point (at least in college). However, if you use a tool that abstracts this (such as Qt Creator, or Linux make files that someone else on the team wrote 5 to 15 years ago) or an alternative (such as Visual Studio C++, C#, Python, Java), then it’s pretty easy to not be aware of this very basic gcc detail. One of my long-term goals is to continue to be a fairly cross-platform OS user and developer (Windows, Mac, Linux / *nix).
I liked this story since it’s a simple example of how spending just a little bit of time (helping a fellow coder – even if he/she is not a hardcore full-time software developer) or (doing a hobby project) put me just a little outside the daily experience of my day job, and enabled me to learn something useful. This was also a great excuse to play some more with Qt Creator.
—
Begin http://www.network-theory.co.uk/docs/gccintro/gccintro_49.html
6.4 Optimization levels
In order to control compilation-time and compiler memory usage, and the trade-offs between speed and space for the resulting executable, GCC provides a range of general optimization levels, numbered from 0–3, as well as individual options for specific types of optimization.
An optimization level is chosen with the command line option -OLEVEL, where LEVEL is a number from 0 to 3. The effects of the different optimization levels are described below:
-O0 or no -O option (default)
- At this optimization level GCC does not perform any optimization and compiles the source code in the most straightforward way possible. Each command in the source code is converted directly to the corresponding instructions in the executable file, without rearrangement. This is the best option to use when debugging a program and is the default if no optimization level option is specified.
-O1 or -O
- This level turns on the most common forms of optimization that do not require any speed-space tradeoffs. With this option the resulting executables should be smaller and faster than with
-O0. The more expensive optimizations, such as instruction scheduling, are not used at this level. Compiling with the option -O1 can often take less time than compiling with -O0, due to the reduced amounts of data that need to be processed after simple optimizations.
-O2
- This option turns on further optimizations, in addition to those used by
-O1. These additional optimizations include instruction scheduling. Only optimizations that do not require any speed-space tradeoffs are used, so the executable should not increase in size. The compiler will take longer to compile programs and require more memory than with -O1. This option is generally the best choice for deployment of a program, because it provides maximum optimization without increasing the executable size. It is the default optimization level for releases of GNU packages.
-O3
- This option turns on more expensive optimizations, such as function inlining, in addition to all the optimizations of the lower levels
-O2 and -O1. The -O3 optimization level may increase the speed of the resulting executable, but can also increase its size. Under some circumstances where these optimizations are not favorable, this option might actually make a program slower.
-funroll-loops
- This option turns on loop-unrolling, and is independent of the other optimization options. It will increase the size of an executable. Whether or not this option produces a beneficial result has to be examined on a case-by-case basis.
-Os
- This option selects optimizations which reduce the size of an executable. The aim of this option is to produce the smallest possible executable, for systems constrained by memory or disk space. In some cases a smaller executable will also run faster, due to better cache usage.
It is important to remember that the benefit of optimization at the highest levels must be weighed against the cost. The cost of optimization includes greater complexity in debugging, and increased time and memory requirements during compilation. For most purposes it is satisfactory to use -O0 for debugging, and -O2 for development and deployment.
End http://www.network-theory.co.uk/docs/gccintro/gccintro_49.html
—
Begin http://stackoverflow.com/questions/796162/g-compiler-flags-optimization-and-flags-for-making-a-static-library-c
The rule of thumb:
When you need to debug, use -O0 (and -g to generate debugging symbols.)
When you are preparing to ship it, use -O2.
When you use gentoo, use -O3…!
When you need to put it on an embedded system, use -Os (optimize for size, not for efficiency.)
End http://stackoverflow.com/questions/796162/g-compiler-flags-optimization-and-flags-for-making-a-static-library-c
—
Pem (Admin) :: 2010/08/05 (Thursday, August 5, 2010) ::
Linux, Qt, Software Development ::
No Comments »