c++ - 编译优化与编译目标的区别是什么

标签 c++ g++ pragma

#pragma GCC optimize()#pragma GCC target()之间的区别是什么?何时选择哪个?其他选项还有哪些?

最佳答案

在较高的层次上,optimize()用于控制在编译代码时是否使用某些优化技术:总体思路是,编译器可以花费更多的时间来生成执行速度更快的代码,或者(有时)花更少的时间编写更紧凑的代码。内存运行。有时,优化的代码有时难以配置或调试,因此您可能希望大多数程序未得到优化或欠优化,但是某些对性能至关重要的特定功能却需要高度优化。实用程序使您可以自由地逐个函数地进行优化。
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options列出并说明了各个优化
最优化属性符号和用途from here的概述:

optimize (level, …)

optimize (string, …)

The optimize attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line. Valid arguments are constant non-negative integers and strings. Each numeric argument specifies an optimization level. Each string argument consists of one or more comma-separated substrings. Each substring that begins with the letter O refers to an optimization option such as -O0 or -Os. Other substrings are taken as suffixes to the -f prefix jointly forming the name of an optimization option. See Optimize Options.

‘#pragma GCC optimize’ can be used to set optimization options for more than one function. See Function Specific Option Pragmas, for details about the pragma.

Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.

Not every optimization option that starts with the -f prefix specified by the attribute necessarily has an effect on the function. The optimize attribute should be used for debugging purposes only. It is not suitable in production code.


target()指示编译器可以将机器代码指令用于特定的CPU。通常,如果您知道运行代码的人可能使用不同代的特定CPU,并且每个代都是向后兼容的,那么您将最早编译这些代,以便每个人都可以运行您的程序。使用target(),您可以利用后代CPU的更高级功能(机器代码指令,或仅针对特定的高速缓存大小或流水线功能进行了优化)来编译不同的函数,然后您的代码可以有选择地在CPU上调用更快的版本,可以支持它。
进一步的细节from here

target (string, …)

Multiple target back ends implement the target attribute to specify that a function is to be compiled with different target options than specified on the command line. One or more strings can be provided as arguments. Each string consists of one or more comma-separated suffixes to the -m prefix jointly forming the name of a machine-dependent option. See Machine-Dependent Options.

The target attribute can be used for instance to have a function compiled with a different ISA (instruction set architecture) than the default. ‘#pragma GCC target’ can be used to specify target-specific options for more than one function. See Function Specific Option Pragmas, for details about the pragma.

For instance, on an x86, you could declare one function with the target("sse4.1,arch=core2") attribute and another with target("sse4a,arch=amdfam10"). This is equivalent to compiling the first function with -msse4.1 and -march=core2 options, and the second function with -msse4a and -march=amdfam10 options. It is up to you to make sure that a function is only invoked on a machine that supports the particular ISA it is compiled for (for example by using cpuid on x86 to determine what feature bits and architecture family are used).

int core2_func (void) __attribute__ ((__target__ ("arch=core2")));

int sse3_func (void) __attribute__ ((__target__ ("sse3")));

Providing multiple strings as arguments separated by commas to specify multiple options is equivalent to separating the option suffixes with a comma (‘,’) within a single string. Spaces are not permitted within the strings.

The options supported are specific to each target; refer to x86 Function Attributes, PowerPC Function Attributes, ARM Function Attributes, AArch64 Function Attributes, Nios II Function Attributes, and S/390 Function Attributes for details.

关于c++ - 编译优化与编译目标的区别是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63217621/

相关文章:

c++ - Visual Studio C++ 中的 DDALine

c++ - 如何在队列开头添加值

perl - 在使用严格之前是否有理由使用警告?

C4996 'scanf' : This function or variable may be unsafe. 考虑改用 scanf_s。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS

c++ - 如何高效实现任意序列的按位旋转?

c++ - 插入后对 vector 进行排序与​​插入到集合中

C++空程序内存泄漏

c++ - 编译器优化 : g++ slower than intel

visual-c++ - Clang 中的编译指示消息?

c++ - Qt: "new without delete"是否会导致控件的内存泄漏?