gpt4 book ai didi

c++ - 针对特定机器/处理器架构进行优化

转载 作者:可可西里 更新时间:2023-11-01 15:09:56 25 4
gpt4 key购买 nike

在这个高度投票的answer关于 C++ 和 Java 之间性能差异的问题,我了解到 JIT 编译器有时能够更好地优化,因为它可以确定机器的确切细节(处理器、缓存大小等):

Generally, C# and Java can be just as fast or faster because the JIT compiler -- a compiler that compiles your IL the first time it's executed -- can make optimizations that a C++ compiled program cannot because it can query the machine. It can determine if the machine is Intel or AMD; Pentium 4, Core Solo, or Core Duo; or if supports SSE4, etc.

A C++ program has to be compiled beforehand usually with mixed optimizations so that it runs decently well on all machines, but is not optimized as much as it could be for a single configuration (i.e. processor, instruction set, other hardware).

问题:有没有办法告诉编译器专门针对我当前的机器进行优化?是否有能够执行此操作的编译器?

最佳答案

对于 GCC,您可以使用标志 -march=native。请注意,生成的代码可能无法在其他 CPU 上运行,因为

GCC uses this name to determine what kind of instructions it can emit when generating assembly code.

因此可以生成特定于 CPU 的程序集。

如果您希望您的代码在其他 CPU 类型上运行,但要对其进行调整以在您的 CPU 上获得更好的性能,那么您应该使用 -mtune=native:

Specify the name of the processor to tune the performance for. The code will be tuned as if the target processor were of the type specified in this option, but still using instructions compatible with the target processor specified by a -mcpu= option.

关于c++ - 针对特定机器/处理器架构进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com