附加的条件声明使程序更快

时间:2012-12-01 20:29:24

标签: c++ performance branch-prediction

在阅读Why is it faster to process a sorted array than an unsorted array?后,我在主循环中添加了一个额外的测试。似乎这个额外的测试使程序更快。

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

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

    //Don't sort the array
    //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];

            //With this additional test, execution becomes faster
            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;
}

我通过额外测试得到大约4.2秒,没有额外测试得到大约18秒。 附加测试不应该使程序变慢而不是让它更快吗?

1 个答案:

答案 0 :(得分:7)

由于特定的附加测试,其等效代码为:

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

        //With this additional test, execution becomes faster
        if (data[c] < 128)
            sum += data[c];
     }
}

成为这个:

for (unsigned i = 0; i < 100000; ++i)
{
  // Primary loop
  for (unsigned c = 0; c < arraySize; ++c)
  {
    sum += data[c];//because exactly one condition is guaranteed to be
                   //true in each iteration (in your code)!
                   //the equivalent is as if there is no condition at all!
  }
}

这就是它变得更快的原因。

由于异常附加测试和相同的主体,编译器能够优化代码,删除if条件。当你有一个if时,编译器就无法做到这一点。

试着写下这个:

sum -= data[c]; //the body is not identical anymore!

处于if条件之一。我确信编译器能够优化代码。它现在应该发出更慢的机器代码。


请注意,外部循环可以完全省略,但它并不太依赖于额外的test ::

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

或者,这个:

// Primary loop
for (unsigned c = 0; c < arraySize; ++c)
{
    sum += data[c];
} 
sum = 100000 * sum; //multiple once!