The GCC compiler offers a number of options that affect the speed and size of the resulting executable. This article shows how you can take advantage of these features.
Optimisation Flags
This sets which level of optimisation is performed on the executable
-O0 No optimisation is performed.
-O1 Performs most common forms of optimisations. The executable should be smaller and faster than with
-O2 Additional optimisation will be used. The executable should not be any larger than with -O1 however compilation will take longer and comsume more memory. This is the current default for GCC
-O3 Enables more expensive optimisations. These optimisations may increase the speed of the executable but will also increase its size. In certain circumstances these optimisations may actually make the executable slower. The only way to know for sure is to experiment!
-Os This enables optimisations that aim to reduce the size of the resulting executable. This is for systems with limited memory and diskspace. The because of size reduction the executable may run faster on these systems also.
-funroll-loops This option turns on loop-unrolling. This will make the executable larger and may increase its speed but this is far from guaranteed. Again, the only way to know is to experiment.
Changing these parameters will affect how the application compiles and may prevent successful compilation in some cases. If your application fails to compile try reducing the level of optimisation used.
System Architecture Flags
By using the -mcpu compiler flag it is possible to take advantage of processor specific optimisations. Examples include:
IA32
-mcpu=pentium4 For Intel Pentium 4 CPUs
-mcpu=athlon For AMD Athlon CPUs
-mcpu=c3 For VIA C3 used in many mini-itx systems
SPARC
-mcpu=supersparc Eg: Sun Sparcstation 20
-mcpu=ultrasparc Eg: Sun Ultra 1, 2, 5, etc
PowerPC
-mcpu=power Generic 32bit PowerPC CPU
-mcpu=power64 Generic 64bit PowerPC CPU
-mcpu=G5 Macintosh G5 system
As an alternative, the -march flag may be used to gain even greater levels of optimisation however it does not support compatibility with other CPUs in the same family so should be used with care.
A full list of supported CPU optimisations can be found in the GCC reference manual: http://gcc.gnu.org/onlinedocs/
Using these flags
To make use of the compilation flags listed above the environment variable CFLAGS should be set, eg:
$ export CFLAGS="-O3 -mcpu=pentium4 -funroll-loops"
If you want these changes to become permanent then add the above line to your ~/.bashrc file
Compliation Threads
The number of simultaneous compilation threads can be specified by the -j option, eg:
$ make -j 4 all
This reduces compile time on multi-processor systems as it can be set to make use of all available CPUs.
Recent Comments