Speed difference between using int and unsigned int when mixed with doubles

后端 未结 4 2013
鱼传尺愫
鱼传尺愫 2021-01-01 14:22

I have an application where part of the inner loop was basically:

double sum = 0;
for (int i = 0; i != N; ++i, ++data, ++x) sum += *data * x;
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 14:41

    Here's some code produced by VC++ 6.0 - no optimisation:

    4:        int x = 12345;
    0040E6D8   mov         dword ptr [ebp-4],3039h
    5:        double d1 = x;
    0040E6DF   fild        dword ptr [ebp-4]
    0040E6E2   fstp        qword ptr [ebp-0Ch]
    6:        unsigned int y = 12345;
    0040E6E5   mov         dword ptr [ebp-10h],3039h
    7:        double d2 = y;
    0040E6EC   mov         eax,dword ptr [ebp-10h]
    0040E6EF   mov         dword ptr [ebp-20h],eax
    0040E6F2   mov         dword ptr [ebp-1Ch],0
    0040E6F9   fild        qword ptr [ebp-20h]
    0040E6FC   fstp        qword ptr [ebp-18h]
    

    As you can see, converting the unsigned does quite a bit more work.

提交回复
热议问题