GCC,比较使用-O2和使用所有启用的优化标志的效果 [英] GCC, compare the effect of using -O2 and using all the optimization flags it turns on
问题描述
来自 gcc5.4文档,它说
-O2打开-O指定的所有优化标志.它还会打开以下优化标志:
-O2 turns on all optimization flags specified by -O. It also turns on the following optimization flags:
-fthread-jumps -falign-functions -falign-jumps -falign-loops -falign-labels -fcaller-保存 跨跳 -fcse-follow-jumps等
-fthread-jumps -falign-functions -falign-jumps -falign-loops -falign-labels -fcaller-saves -fcrossjumping -fcse-follow-jumps, etc
使用-O2似乎与使用gcc 5.4.0中-O2打开的所有83个优化标志对测试程序的性能具有相同的效果.
It seems that using -O2 has the same effect of using all the 83 optimization flags turned on by -O2 in gcc 5.4.0 on the performance of the test programs.
但是,我比较了
gcc-5.4 -O2 test.c -o test1
gcc-5.4 -O2 test.c -o test1
和
gcc-5.4 -fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments -fcompare-elim ... -fthread-jumps -falign-functions ...(所有83个标志)test.c -o test2
gcc-5.4 -fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments -fcompare-elim ... -fthread-jumps -falign-functions ...(all the 83 flags) test.c -o test2
我在20个随机生成的c程序上进行了测试,并且每个测试用例运行了100000次,以确保运行时间的测量足够准确. 但是结果是,使用-O2比使用所有83个标志平均快60%.
I tested on 20 random generated c programs and running each test case 100000 times to make sure the measurement of running time is accurate enough. But the result is that using -O2 is averagely about 60% faster than using all the 83 flags.
我真的很困惑,为什么使用-O2的效果并不等同于使用它打开的所有优化标志.
I am really confused why the effect of using -O2 is not equivalent to using all the optimization flags it turns on.
我必须误解了一些东西,但是我还找不到任何解释.我将不胜感激.非常感谢.
I must misunderstood something, but I couldn't find any explanation yet. I'd appreciate any help. Thanks a lot.
推荐答案
这是常见的陷阱.为了启用(或禁用)特定的优化,必须首先总体上启用优化器,即使用-O...
标志之一,但-O0
除外(或仅使用-O
,其等效于-O1
).
It is a common gotcha. In order to enable (or disable) specific optimizations, you must first enable the optimizer in general, i.e. use one of -O...
flags, except -O0
(or just -O
, which is equivalent to -O1
).
这篇关于GCC,比较使用-O2和使用所有启用的优化标志的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!