姬長信(Redy)

c – gcc优化标志-O3使代码慢于-O2


我找到这个话题Why is it faster to process a sorted array than an unsorted array?.并尝试运行此代码.而且我发现了奇怪的行为.如果我使用-O3优化标志编译此代码,则需要2.98605秒才能运行.如果我用-O2编译它需要1.98093秒.我尝试在同一环境中的同一台机器上运行此代码几次(5或6),我关闭所有其他软件(chrome,skype等).
gcc --version
gcc (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

那么请你能解释一下为什么会这样吗?我读了gcc手册,我看到-O3包括-O2.谢谢你的帮助.

附:添加代码

#include 
#include 
#include 

int main()
{
    // Generate data
    const unsigned arraySize = 32768;
    int data[arraySize];

    for (unsigned c = 0; c = 128)
                sum += data;
        }
    }

    double elapsedTime = static_cast(clock() - start) / CLOCKS_PER_SEC;

    std::cout