使用 STL 向量优化算术运算



我有一些简单的结构:

struct ab { double a,b; }
struct abcd { double a,b,c,d; }
struct ch
{
...
  std::vector<abcd> x;
  std::vector<size_t> ir;
...
}

和代码:

ch l;
std::vector<ab> x;
double c,f;
...
for(size_t i = ... )
{
    ...
    l.x[i].c = (l.x[i].c / c) + f*x[l.ir[i]].a; // line#1
    ...
}

CodeXl 显示最昂贵的行之一是 line #1。和 60% 的行 #1 采取

 mov eax,[edx+eax]

如何优化行#1?

为什么"移动"操作比 mul 和div 更昂贵?

更新从 CodeXl 完全反编译第 #1 行:

l.x[i].c = (l.x[i].c / c) + f*x[l.ir[i]].a; => 15.871% of function time
;;
mov ecx,[ebx+4ch]
lea edx,[edi*4+00000000h] => 0.99194%
shl edi,05h
mov eax,[ebx+1ch]
movsd xmm0,[ecx+edi+10h]
divsd xmm0,xmm2 => 1.17793%
mov eax,[edx+eax] => 10.0434%
add eax,eax
movsd xmm1,[esi+eax*8]
mulsd xmm1,xmm4
addsd xmm1,xmm0 => 1.30192%
movsd [ecx+edi+10h],xmm1 => 2.35586%

更新Microsoft Visual Studio 2013。版本32

muldiv都很快,因为参数可用。 mov eax, [eax+edx]需要内存中的参数。它是在缓存中还是预取?我怀疑这个特定的mov来自您的x[l.ir[i]]表达式,x足够大以取消缓存,并且l.ir[i]足够非线性以击败预取器。这意味着您正在等待主内存。

最新更新