You can turn off all optimizations for specific functions by using #pragma optimize. Valid second arguments for #pragma optimize are as shown below:
#pragma optimize("", off) - disables optimization
#pragma optimize("", on) - enables optimization
The compiler ignores first argument values.
Specifying #pragma optimize("", off)disables optimization until either the compiler finds a matching #pragma optimize("", on) statement or until the compiler reaches the end of the source file. For example, in example 1 (below), optimization is disabled for function alpha() but not for function omega().
Example 1: Disabling optimization for a single function |
---|
#pragma optimize("",
off) #pragma optimize("", on) omega() { ... } |
In example 2, optimizations are disabled for both the alpha() and omega()functions.
Example 2: Disabling optimization for all functions |
---|
#pragma optimize("",
off) omega() { ... } |
You can control optimization for a specific function using #pragma optimization_level. This pragma affects optimization for the specified function only. You can use this pragma to restrict optimization for a specific function while optimizing the remaining application using a different, higher optimization level. For example, if you specify -O3 (Linux*) or /O3 (Windows*) for the application and specify #pragma optimization_level 1, the marked function will be optimized at the -O1 (Linux) or /O1 (Windows) level, while the remaining application will be optimized at the higher level.
The pragma uses the following syntax:
Syntax |
---|
#pragma optimization_level n |
where n is an integer value. The valid values and descriptions are shown below.
Value |
Description |
---|---|
0 |
Same as -O0 (Linux) or /Od (Windows) |
1 |
Same as -O1 (Linux) or /O1 (Windows) |
2 |
Same as -O2 (Linux) or /O2 (Windows) |
3 |
Same as -O3 (Linux) or /O3 (Windows) |
For more information on the optimizations levels, see Optimization Options Summary.
Place the pragma immediately prior to the function, as shown below.
Example |
---|
#pragma optimization_level 1 } |
In general, the pragma will optimize the function at the level specified for n; however, certain compiler optimizations, like IPO, are not enabled or disabled during translation unit compilation. For example, if you enable IPO and a specific optimization level, IPO is enabled even for the function targeted by the optimization_level pragma, although it might not be fully implemented regardless of the optimization level specified at the command line. The reverse is also true.