The latest version of this topic can be found at 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.數組
Optimization Pragmas and Keywords
A list of keywords and pragmas that you can use in your code to improve performance.多線程
Compiler Options Listed by Category
A list of /O compiler options that specifically affect execution speed or code size.app
Rvalue Reference Declarator: &&
Rvalue references support the implementation of move semantics. If move semantics are used to implement template libraries, the performance of applications that use those templates can significantly improve.ide
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)
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.
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.