Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 85

Code generation and optimization

Оглавление

Every compiler converts source code into machine language, but some compilers can translate source code into more efficient machine language commands than other compilers. As a result, it’s possible to compile the same C++ program under two different C++ compilers and create identically working programs that consist of different machine language instructions.

The goal of every compiler is to create a program that takes up as little memory and disk space as possible while running as fast as possible. Usually, compilers make a trade-off. To make a program run faster, the executable file may take up a large amount of disk space or require a lot of memory. If the compiler can reduce the size of your program and the amount of memory it needs to run, it may create a slow program.

To help you tweak your program for the best balance of speed, size, and memory requirements, many compilers offer optimization settings. By fiddling with these optimization settings, you can tell the compiler how to speed up or shrink your program, as shown in Figure 4-1.


FIGURE 4-1: Compiler optimization settings let you make your program as small and as fast as possible.

One major feature of a compiler’s code generation capabilities involves speed, which can measure two different features:

 How quickly the compiler works in translating your source code to machine code: In the old days, compilers could take hours or days to compile a simple program. Nowadays, compilers often work in minutes or even seconds. Shove in a program that consists of 800,000 lines of code, and in less than a minute, the compiler can create an executable file for you. The faster the compiler works, the less time you waste waiting to run and test your program.

 The performance of the machine language code that the compiler creates: Given the same program, one compiler may create a program that runs quickly, whereas a second compiler may create that same program that runs much slower.

Ideally, you want a compiler that both compiles fast and creates programs that run fast.

Beginning Programming All-in-One For Dummies

Подняться наверх