c++ - 英特尔编译器生成的代码比 MSVC 慢 68%(提供了完整示例)

标签 c++ performance optimization cpu icc

我让 C++ 代码处理来自一个 1800 元素数组的三个连续值。 ICC 14.0 编译的代码比 MSVC 生成的代码慢大约 68%(1600 对 2700 个 CPU 周期)。我不明白为什么。有人可以帮忙吗?即使我设置了 Intel 编译器 -O3 开关,它也不会改变时间。 CPU 是 Ivy Bridge。

#include <iostream>

int main(){
        int data[1200];

        //Dummy-populate data
        for(int y=0; y<1200; y++){
            data[y] = y/2 + 7;
        }

        int counter = 0;

        //Just to repeat the test
        while(counter < 10000){

            int Accum = 0;
            long long start = 0;
            long long end = 0;
            int p = 0;

            start = __rdtsc();

            while(p < 1200){
                unsigned int level1 = data[p];  
                unsigned int factor = data[p + 1];
                Accum += (level1 * factor);
                p = p + 2;
            }

            end = __rdtsc();
            std::cout << (end - start) << "  " << Accum << std::endl;
            counter++;
        }
}

最佳答案

ICC 在这里很糟糕,因为它正在计算每个 data[n] 访问 ala mov edi,dword ptr [rsp+rax*4+44h] 的地址。 . 所有运行时乘法都很昂贵。您应该能够通过重新编码来避免它,因此索引是常量(也可以使用 *p_data++ 三次,但这会引入可能对性能产生不利影响的排序问题)。

for (unsigned* p_data = &data[0], *p_end = data + 1800; p_data < p_end; p_data += 3)
{
    unsigned level1 = p_data[0];
    unsigned level2 = p_data[1];
    unsigned factor = p_data[2];

    Accum1 += level1 * factor;
    Accum2 += level2 * factor;
}

关于c++ - 英特尔编译器生成的代码比 MSVC 慢 68%(提供了完整示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531013/

相关文章:

javascript - Underscore.js 为什么使用 "switch' 而不是 "for loop"来处理案例

c++ - 返回 std::pair 与通过非常量引用传递

c++ - GLSL 玻璃效果加深度剥离

c++ - 复制文件时出现段错误

java - 我的第一个 LWJGL 应用程序性能不佳

c# - ConfigurationManager.AppSettings 性能问题

c - 为什么 BSS 中静态数组的第二个循环比第一个更快?

c++ - 需要理解语句 "Accessability is checked statically and not dynamically in C++"

c++ - delete 和 delete[] 是否等同于基本数据类型

c# - 在 C# 中,在算法中使用递归函数是一种好习惯吗?