V4O.o.1o2是什么系统版本v4.02.r11

2015计算机一级考的是什么版本?_百度知道GCC中-O1 -O2 -O3 优化的原理是什么?
做OI时候发现C++开了O2优化后可以在不超时的情况下随便用STL,非常好奇这是怎么实现的?
俺来打个酱油。后面有谁来展开解答这个话题就功德无量了。 大大讲了C++的语言和STL的一侧。这只覆盖了要优化的对象但没覆盖如何优化。 大大讲了编译器优化话题太巨大。话题是巨大,不过总有切入点。楼主只是想大概知道些名词但不想太深入的话,可以先从GCC的文档入手:这份文档讲解了每层-O参数对应哪些具体的优化。每个具体的优化参数是“-f + 优化名”,例如-fdce的优化名是DCE,也就是dead code elimination。知道优化的名字就可以去搜搜相关资料了。唯一的问题就是GCC用的这些优化名称可能跟教科书叫法不一样? 例如-fmove-loop-invariant在其它资料里可能更多叫做loop-invariant code motion(LICM)。更高层一点,还可以从文档中了解一下GCC的工作流程:
-On(n=0,1,2,3,也可以是其它单词)是gcc为了一般人方便而做的设定,根据n值大小包含预设标准由低到高的一些优化选项,均为-fxxx(xxx为优化项),但注意,即使是最高优化选项-O3,也不是包含所有的-f选项,这只是为大多数人的使用方便而预设的。 -O0: This level (that is the letter "O" followed by a zero) turns off optimization entirely and is the default if no -O level is specified in CFLAGS or CXXFLAGS. This reduces compilation time and can improve debugging info, but some applications will not work properly without optimization enabled. This option is not recommended except for debugging purposes. -O1: the most basic optimization level. The compiler will try to produce faster, smaller code without taking much compilation time. It is basic, but it should get the job done all the time. -O2: A step up from -O1. The recommended level of optimization unless the system has special needs. -O2 will activate a few more flags in addition to the ones activated by -O1. With -O2, the compiler will attempt to increase code performance without compromising on size, and without taking too much compilation time. -O3: the highest level of optimization possible. It enables optimizations that are expensive in terms of compile time and memory usage. Compiling with -O3 is not a guaranteed way to improve performance, and in fact, in many cases, can slow down a system due to larger binaries and increased memory usage. -O3 is also known to break several packages. Using -O3 is not recommended. -Os: optimizes code for size. It activates all -O2 options that do not increase the size of the generated code. It can be useful for machines that have extremely limited disk storage space and/or CPUs with small cache sizes. -Og: In GCC 4.8, a new general optimization level, -Og, has been introduced. It addresses the need for fast compilation and a superior debugging experience while providing a reasonable level of runtime performance. Overall experience for development should be better than the default optimization level -O0. Note that -Og does not imply -g, it simply disables optimizations that may interfere with debugging. -Ofast: New in GCC 4.7, consists of -O3 plus -ffast-math, -fno-protect-parens, and -fstack-arrays. This option breaks strict standards compliance, and is not recommended for use.你开了-O2,stl里大量的函数都被inline了,故而比原来快好多,毋庸置疑,stl没有inline基本上是威力全无,切记。虽然-O3的建议程度不比-O2,但个人推荐-O3作为release版本的选项,因为它开启了重要的-finline-functions,对于实在是要在乎code size的情况,配合使用noinline。这总比费劲心思profiling然后写inline函数来得简单。
C++的template就设计成,只要你开了优化就可以把封装带来的中间层去掉的形式。因此STL拼命的抽象,然后你开了O2,这些抽象编译后就“不占地方”了,就跟你直接用C为每个类型精心设计的容器类型一样。当然C++显然方便多了。但是你不开O2,你就能感受到那些抽象带来的效果。
有区别的一个个试过来就是了嘛,比如gcc -Q -O1 --help=optimizers
举个不恰当的例子,编译器和这个净水器类似,包含有很多代码优化模块,但是为了不让你选择到底打开哪些优化模块(给你选你未必也看得懂),所以一般有这么几种选择:自来水,矿泉水,纯净水,蒸馏水,恒大冰泉,依云。这就够了。
总体来说,优化的目标主要是更小的目标文件和更短的运行速度。此外,编译本身需要的内存和处理器资源也是需要考虑的。不幸的是,很多时候这些目标是互相矛盾的,所以有了O[s123]这些选项。比如,O3会做更激进的循环展开, 函数内联, 更耗时的指针别名分析, 更复杂的指令模式匹配 (比如指令的向量化),
更耗时的指令调度, 寄存器分配。 甚至做好几遍:寄存器分配前调度一次,之后再调度一次。最基本的O0就是最简单的编译,通常用于Debug版本。更高级的还有链接时优化(全程序)、PGO
已有帐号?
无法登录?
社交帐号登录Directx版本是什么意思?_百度知道汽车版本是什么_百度知道dota最早的版本是什么?谁做的?_百度知道

参考资料

 

随机推荐