GCC建议和快速代码的选项 [英] GCC recommendations and options for fastest code

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

问题描述

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



到目前为止,我有 g ++ -O3 -Wno-write-strings 我应该添加其他选项吗?在Windows上,Microsoft编译器提供了一些值得使用的选项,例如快速调用约定和链接时间代码生成,gcc上是否有等效项?



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

解决方案

不知道程序的任何细节,很难说。 O3涵盖了大多数优化。其余的选择是付出一定代价的。如果您可以容忍一些随机取整并且您的代码不依赖于IEEE浮点标准,则可以尝试-Ofast。


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


另一个选项是启用C99(-std = c99)并内联适当的功能。这有点艺术,您不应该内联所有内容,但是通过一点工作,您就可以使代码更快(尽管以拥有更大的可执行文件为代价)。



编辑:哦,等等,您说的是C ++?然后无视我的C99段落,您可以已经内联:)


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.

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?

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

解决方案

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.

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.

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).

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.

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

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

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