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.
The following topics describe some of the optimization features in the C/C++ language.
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)
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.
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.
-
For more information about how to reduce the time to load DLL methods, see "Optimizing DLL Load Time Performance" in the "Under the Hood" column in the "MSDN Magazine" on the MSDN Library Web site.
-
For more information about how to minimize paging in applications, see "Improving Runtime Performance with the Smooth Working Set Tool" and "Improving Runtime Performance with the Smooth Working Set Tool—Part 2" in the "Bugslayer" column in the "MSDN Magazine" on the MSDN Library Web site.