Delen via


Optimizing Your Code

By optimizing an executable, you can achieve a balance between fast execution speed and small code size. This topic discusses some of the mechanisms that Visual C++ provides to help you optimize code.

Language Features

The following topics describe some of the optimization features in the C/C++ language.

The optimize Pragma

If an optimized section of code causes errors or a slowdown, you can use the optimize pragma to turn off optimization for that section.

Enclose the code between two pragmas, as follows.

#pragma optimize("", off)
// some code here 
#pragma optimize("", on)

Programming Practices

You might notice additional warning messages when you compile your code with optimization. This behavior is expected because some warnings relate only to optimized code. You can avoid many optimization problems if you heed these warnings.

Paradoxically, optimizing a program for speed could cause code to run slower. This is because some optimizations for speed increase code size. For example, inlining functions eliminates the overhead of function calls. However, inlining too much code might make your program so large that the number of virtual-memory page faults increases. Therefore, the speed gained from eliminating function calls may be lost to memory swapping.

The following topics discuss good programming practices.

  • Tips for Improving Time-Critical Code
    Better coding techniques can yield better performance. This topic suggests coding techniques that can help you make sure that the time-critical parts of your code perform satisfactorily.

  • Optimization Best Practices
    Provides general guidelines about how best to optimize your application.

Debugging Optimized Code

Because optimization might change the code created by the compiler, we recommend that you debug your application and measure its performance, and then optimize your code.

The following topics provide basic information about how to debug.

The following topics provide more advanced information about how to debug.

The following assortment of topics provide information about how to optimize building, loading, and executing your code.

See Also

Other Resources

C/C++ Building Reference