[英]optimize arithmetic operations with stl vector
我有一些簡單的結構:
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
...
}
CodeX1顯示最昂貴的行之一是第1行。 第1行的60%
mov eax,[edx+eax]
如何優化第1行?
為什么“ mov”操作比mul和div更昂貴?
UPD從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 Studio2013。Release32
mul
和div
快速,因為參數可用。 mov eax, [eax+edx]
需要內存中的參數。 是在緩存中還是預取? 我懷疑這個特定的mov
來自您的x[l.ir[i]]
表達式, x
足夠大l.ir[i]
無法緩存,而l.ir[i]
則足夠非線性l.ir[i]
無法擊敗預取器。 這意味着您正在等待主內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.