gcc选项的最快代码 [英] gcc options for fastest code

查看:114
本文介绍了gcc选项的最快代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Unix版本分发一个带有makefile的C ++程序,我想知道应该使用什么编译器选项来获得最快的代码(它属于可以使用所有计算能力的程序类别他们可以得到,仍然回来为更多),鉴于我不提前知道用户将有什么硬件,操作系统或gcc版本,我想要的最重要的是,确保它至少在每个主要工作正常类似Unix的操作系统。

I'm distributing a C++ program with a makefile for the Unix version, and I'm wondering what compiler options I should use to get the fastest possible code (it falls into the category of programs that can use all the computing power they can get and still come back for more), given that I don't know in advance what hardware, operating system or gcc version the user will have, and I want above all else to make sure it at least works correctly on every major Unix-like operating system.

到目前为止,我有 g ++ -O3 -Wno-write-strings 有什么其他选择我应该添加?在Windows上,微软编译器有一些选项,比如快速调用约定和链接时间代码生成,值得使用,gcc上有什么等同的东西吗?

Thus far, I have g++ -O3 -Wno-write-strings, are there any other options I should add? On Windows, the Microsoft compiler has options for things like fast calling convention and link time code generation that are worth using, are there any equivalents on gcc?

假设它在64位平台上默认为64位,请纠正我,如果不是这样。)

(I'm assuming it will default to 64-bit on a 64-bit platform, please correct me if that's not the case.)

推荐答案

不知道你的程序的任何细节很难说。 O3涵盖了大多数优化。剩余的选项有代价。如果你可以容忍一些随机舍入和你的代码不依赖于IEEE浮点标准,那么你可以尝试-Ofast。这会忽略标准合规性,并可以为您提供更快的代码。

Without knowing any specifics on your program it's hard to say. O3 covers most of the optimisations. The remaining options come "at a cost". If you can tolerate some random rounding and your code isn't dependent on IEEE floating point standards then you can try -Ofast. This disregards standards compliance and can give you faster code.

剩余的优化标志只能提高某些程序的性能,甚至可能对他人有害。查看 gcc关于优化标记的文档中的可用标记,并对其进行基准化。

The remaining optimisations flags can only improve performance of certain programs, but can even be detrimental to others. Look at the available flags in the gcc documentation on optimisation flags and benchmark them.

另一个选项是启用C99(-std = c99)和内联适当的函数。这是一个艺术,你不应该内联一切,但有一点工作,你可以让你的代码更快(虽然以更大的可执行文件为代价)。

Another option is to enable C99 (-std=c99) and inline appropriate functions. This is a bit of an art, you shouldn't inline everything, but with a little work you can get your code to be faster (albeit at the cost of having a larger executable).

如果速度真的是一个问题,我建议或者回到微软的编译器,或者尝试英特尔的。我来欣赏一下gcc编译代码有多慢,尤其是涉及到math.h时。

If speed is really an issue I would suggest either going back to Microsoft's compiler, or to try Intel's. I've come to appreciate how slow some gcc compiled code can be, especially when it involves math.h.

编辑:哦,等等,你说C ++?然后忽略我的C99段落,你可以内联:)

Oh wait, you said C++? Then disregard my C99 paragraph, you can inline already :)

这篇关于gcc选项的最快代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆