Improving Performance with Compile Time Options

If you compile your own software, you can pass options to the compiler to have it optimize the resulting source code for a specific CPU model. For instance, if you have an Athlon, you might compile your software with Athlon optimizations. For most programs, these optimizations will yield only trivial performance improvements, but for some programs, the benefits can be more important. These critical packages include the Linux kernel, common system libraries such as libc, any CPU-intensive programs that your system runs frequently, and for workstations your X server and other core GUI tools. Most IA-32 distributions ship with ¡386 optimizations, which means the distributions work even on very old computers, but they may experience modest performance improvements if you recompile some of these critical components. Some distributions offer prebuilt kernel packages with CPU-specific optimizations, so you may want to use these kernels. Mandrake is unusual in that it builds everything with ¡586 optimizations, which provides a small performance boost.

If you want to compile your own software with optimizations, you need to pass the appropriate options to the compiler. The GNU Compiler Collection (GCC) supports two options for IA-32 CPU optimizations: -mcpu and -march. The former optimizes code for a specific CPU model, but it does so in a way that allows code to run on other CPU models. The latter option does more complete optimizations for a CPU in such a way that the code may not run on other (and particularly on older) CPUs. Both options take the name of a CPU as a parameter, as in -march=pentium4. Options are ¡386, i486, ¡586, ¡686, pentium, pentium-mmx, pentiumpro, pentium2, pentium3, pentium4, k6, k6-2, k6-3, athlon, athlon-tbird, athlon-4, athlon-xp, and athlon-mp. The ¡586 option is equivalent to pentium, and ¡686 is equivalent to pentiumpro.

In order to use these options, you can either compile code directly using GCC (as in gcc -march=pentium4 myprog.c -o myprog) or edit the Makefile for a package. Typically, a Makefile includes a line in which various GCC flags are defined, such as

CFLAGS= -02 -Wall -l/usr/X11 R6/include

You should change this line to add the appropriate optimization, as in: CLFAGS= -02 -march=pentium4 -Wall -l/usr/X11 R6/include

In some cases, a Makefile will already specify an optimization, in which case you can change that default. Some packages have configuration tools that enable you to specify an optimization. Consult the package's documentation for details. (The next section, "Setting Kernel Options," includes information on optimizing the Linux kernel.)

In addition to CPU-specific optimizations, it's possible to tell GCC to optimize code in less CPU-specific ways. These optimizations may increase compile time, reduce the reliability of the debugger, or increase program size. These optimizations are specified with the -0 (that's an uppercase letter 0) option, followed by a number from 0 to 3, as in -02 in the preceding examples. Increasing numbers represent increasing optimizations for execution speed. In addition, the -Os option optimizes the code to reduce its size.

Note Most distributions today use Red Hat Package Manager (RPM) or Debian packages. (Slackware is a notable exception.) Rebuilding such software from source code packages may or may not optimize them for your CPU, depending upon the build scripts included in the source package. You may need to edit these scripts in order to rebuild a package with CPU-specific optimizations.

0 0

Post a comment