Muokkaa

Jaa


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 Studio provides to help you optimize code.

Language features

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.

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.

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 shown here:

#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 information about how to debug release builds.

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

In this section

Optimization Pragmas and Keywords
Improving Compiler Throughput
Why Floating-Point Numbers May Lose Precision
IEEE Floating-Point Representation
Tips for Improving Time-Critical Code
Using Function Name Without () Produces No Code
Optimization Best Practices
Profile-Guided Optimizations
Environment Variables for Profile-Guided Optimizations
PgoAutoSweep
pgomgr
pgosweep
How to: Merge Multiple PGO Profiles into a Single Profile

See also

C/C++ Building Reference