CS 112 Lab Exercise: Faster Genome Scanning: Optimization


Compiler Optimization

In the provided Makefile, the C++ compiler uses this command to build genScan:

   g++ genScan.cpp -Wall -ansi -pedantic -std=c++11 -o genScan -fopenmp
In this command: What is not obvious is that this compile-command tells g++ to build our program as quickly as possible, even if doing so results in slow performance.

We can change that by tweaking the Makefile. Using your text editor, open Makefile, find the line that looks like this:

   CFLAGS = -Wall -ansi -std=c++11
and change it to read:
   CFLAGS = -O2 -Wall -ansi -std=c++11

Compiler optimization is having the compiler spend extra time translating our program into the native binary code, looking for ways to make the binary program faster, smaller, or both.

The GNU compilers support four optimization levels:
-O0 (level zero) performs no optimizations; the compiler just builds the program as fast as possible. This is g++'s default setting if the user doesn't specify a level.
-O1 (level one) performs simple optimizations--improvements that won't slow down the build very much.
-O2 (level two) performs all optimizations that will not increase the size of the binary program.
-O3 (level three) performs all available optimizations.

The change we just made to the Makefile will cause g++ to perform level two optimization instead of level zero, which nearly always speeds up the resulting program.


CS > 112 > Labs > 12 > Optimization
This page maintained by Joel Adams.