简体   繁体   English

使用stl向量优化算术运算

[英]optimize arithmetic operations with stl vector

I have some simple structs: 我有一些简单的结构:

struct ab { double a,b; }
struct abcd { double a,b,c,d; }

struct ch
{
...
  std::vector<abcd> x;
  std::vector<size_t> ir;
...
}

And code: 和代码:

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 shows that one of the most expensive lines is line#1. CodeX1显示最昂贵的行之一是第1行。 And 60% of line#1 take 第1行的60%

 mov eax,[edx+eax]

How can I optimize line#1? 如何优化第1行?

Why "mov" operation more expensive than mul and div? 为什么“ mov”操作比mul和div更昂贵?

Upd Full decompiling of line#1 from CodeXl: 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%

Upd Microsoft Visual Studio 2013. Release32 更新了 Microsoft Visual Studio2013。Release32

mul and div are fast because the arguments are available. muldiv快速,因为参数可用。 mov eax, [eax+edx] requires an argument from memory. mov eax, [eax+edx]需要内存中的参数。 Is it in cache or prefetched? 是在缓存中还是预取? I suspect this particular mov is from your x[l.ir[i]] expression, x is sufficiently large to be uncached, and l.ir[i] is sufficiently non-linear to defeat the prefetcher. 我怀疑这个特定的mov来自您的x[l.ir[i]]表达式, x足够大l.ir[i]无法缓存,而l.ir[i]则足够非线性l.ir[i]无法击败预取器。 That means you're waiting for main memory. 这意味着您正在等待主内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM