我找到了这个话题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;
}