大佬教程收集整理的这篇文章主要介绍了为什么为x64平台编译c程序比x86编译慢?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "timer.h" #include <vector> #include <iostream> #include <algorithm> #include <String> #include <sstream> /********************DECLARATIONS************************************************/ class Vector { public: Vector():x(0),y(0),z(0){} Vector(double x,double y,double z) : x(X),y(y),z(z) { } double x; double y; double z; }; double Dot(const Vector& a,const Vector& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } class Vector2 { public: typedef double value_type; Vector2():x(0),y(0){} Vector2(double x,double y) : x(X),y(y) { } double x; double y; }; /******************************TESTS***************************************************/ void Test(const std::vector<Vector>& m,std::vector<Vector2>& m2) { Vector axisX(0.3f,0.001f,0.25f); Vector axisY(0.043f,0.021f,0.45f); std::vector<Vector2>::iterator i2 = m2.begin(); std::for_each(m.begin(),m.end(),[&](const Vector& v) { Vector2 r(0,0); r.x = Dot(axisX,v); r.y = Dot(axisY,v); (*i2) = r; ++i2; }); } int main() { cpptask::Timer timer; int len2 = 300; size_t len = 5000000; std::vector<Vector> m; m.reserve(len); for (size_t i = 0; i < len; ++i) { m.push_BACk(Vector(i * 0.2345,i * 2.67,i * 0.98)); } /***********************************************************************************/ { std::vector<Vector2> m2(m.size()); double time = 0; for (int i = 0; i < len2; ++i) { timer.Start(); Test(m,m2); time += timer.End(); } std::cout << "Dot product double - " << time / len2 << std::endl; } /***********************************************************************************/ return 0; }
长答案:
如果SSE2被禁用,这个x86版本很慢.但是我可以在x86中启用SSE2来重现结果.
如果你潜入最内圈循环的装配. x64版本最后有两个额外的内存副本.
86:
$LL71@main: movsd xmm2,QWORD PTR [eax-8] movsd xmm0,QWORD PTR [eax-16] movsd xmm3,QWORD PTR [eax] movapd xmm1,xmm0 mulsd xmm0,QWORD PTR __real@3fa60418a0000000 movapd xmm7,xmm2 mulsd xmm2,QWORD PTR __real@3f95810620000000 mulsd xmm7,xmm5 mulsd xmm1,xmm4 addsd xmm1,xmm7 movapd xmm7,xmm3 mulsd xmm3,QWORD PTR __real@3fdcccccc0000000 mulsd xmm7,xmm6 add eax,24 ; 00000018H addsd xmm1,xmm7 addsd xmm0,xmm2 movq QWORD PTR [ecx],xmm1 addsd xmm0,xmm3 movq QWORD PTR [ecx+8],xmm0 lea edx,DWORD PTR [eax-16] add ecx,16 ; 00000010H cmp edx,esi jne SHORT $LL71@main
64位:
$LL175@main: movsdx xmm3,QWORD PTR [rdx-8] movsdx xmm5,QWORD PTR [rdx-16] movsdx xmm4,QWORD PTR [rdx] movapd xmm2,xmm3 mulsd xmm2,xmm6 movapd xmm0,xmm5 mulsd xmm0,xmm7 addsd xmm2,xmm0 movapd xmm1,xmm4 mulsd xmm1,xmm8 addsd xmm2,xmm1 movsdx QWORD PTR r$109492[rsp],xmm2 mulsd xmm5,xmm9 mulsd xmm3,xmm10 addsd xmm5,xmm3 mulsd xmm4,xmm11 addsd xmm5,xmm4 movsdx QWORD PTR r$109492[rsp+8],xmm5 mov rcx,QWORD PTR r$109492[rsp] mov QWORD PTR [rax],rcx mov rcx,QWORD PTR r$109492[rsp+8] mov QWORD PTR [rax+8],rcx add rax,16 add rdx,24 lea rcx,QWORD PTR [rdx-16] cmp rcx,rbx jne SHORT $LL175@main
x64版本在循环结束时有更多(无法解释的)动作.它看起来像某种内存到内存数据副本.
编辑:
原来,x64优化器无法优化以下副本:
(*i2) = r;
这就是为什么内部循环有两个额外的内存副本.如果将循环更改为:
std::for_each(m.begin(),[&](const Vector& v) { i2->x = Dot(axisX,v); i2->y = Dot(axisY,v); ++i2; });
这消除了副本.现在x64版本和x86版本一样快:
x86: 0.0249423 x64: 0.0249348
经验教训:编译器不完美.
以上是大佬教程为你收集整理的为什么为x64平台编译c程序比x86编译慢?全部内容,希望文章能够帮你解决为什么为x64平台编译c程序比x86编译慢?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。