gcc优化标志-O3使代码比-O2慢

时间:2015-03-05 10:17:38

标签: c++ gcc optimization

我找到了这个话题Why is it faster to process a sorted array than an unsorted array?。并尝试运行此代码。而且我发现了奇怪的行为。如果我使用-O3优化标志编译此代码,则运行2.98605 sec。如果我使用-O2进行编译,则需要1.98093 sec。我尝试在相同的环境中在同一台机器上运行此代码几次(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。谢谢你的帮助。

P.S。添加代码

#include <algorithm>
#include <ctime>
#include <iostream>

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

    for (unsigned c = 0; c < arraySize; ++c)
        data[c] = std::rand() % 256;

    // !!! With this, the next loop runs faster
    std::sort(data, data + arraySize);

    // Test
    clock_t start = clock();
    long long sum = 0;

    for (unsigned i = 0; i < 100000; ++i)
    {
        // Primary loop
        for (unsigned c = 0; c < arraySize; ++c)
        {
            if (data[c] >= 128)
                sum += data[c];
        }
    }

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

    std::cout << elapsedTime << std::endl;
    std::cout << "sum = " << sum << std::endl;
}

0 个答案:

没有答案