简体   繁体   English

优化“for”循环

[英]Optimize “for” loop

std::vector<int> someVector;    
for (unsigned int i = 0; i < someVector.size(); i++)
{
   // do something
}

Does the value of someVector.size() get calculated every time ? someVector.size()的值是否每次计算?

I have checked with GCC explorer : 我已经与GCC资源管理器核对过:

Code entered: 输入的代码:

#include<vector>

int sum(const std::vector<int> & someVector) {
  int s = 0;
  for (int i = 0; i < someVector.size(); i++) {
    s += someVector[i];
  }
  return s;
}

int main() {
  std::vector<int> someVector;
  return sum(someVector);
};

Assembly generated for sum(): 为sum()生成的汇编:

  movq  (%rdi), %rcx
  movq  8(%rdi), %rdx
  xorl  %eax, %eax
  cmpq  %rcx, %rdx
  je    .LBB0_3
  subq  %rcx, %rdx
  sarq  $2, %rdx
  xorl  %eax, %eax
  xorl  %esi, %esi
.LBB0_2:                                # =>This Inner Loop Header: Depth=1
  addl  (%rcx,%rsi,4), %eax
  incq  %rsi
  cmpq  %rdx, %rsi
  jb    .LBB0_2
.LBB0_3:                                # %._crit_edge
  ret

ie the size is kept in %rdx -- there is no call to size() each time. 即大小保持在%rdx - 每次都没有调用size()

As others have pointed out already, results may depend on 正如其他人已经指出的那样,结果可能取决于

  • your compiler, 你的编译器,
  • optimization settings and 优化设置和
  • what you actually do in the loop (click the gcc explorer link above to try yourself). 你在循环中实际做了什么(单击上面的gcc explorer链接尝试自己)。

Without calculating anything, the whole loop gets optimized away. 没有计算任何东西,整个循环就会被优化掉。

Does the value of someVector.length() get calculated every time? 是否每次都计算someVector.length()的值?

Possibly , depending on the contents of the loop and many other things. 可能 ,取决于循环的内容和许多其他事情。 For instance, the size of the vector could be modified inside the loop. 例如,可以在循环内修改向量的大小。 Then your compiler has to be able to spot if this is not the case. 然后你的编译器必须能够发现是不是这种情况。 So a few conditions have to be met for this to be optimized out. 因此,必须满足一些条件才能进行优化。

If you require that std::vector::size() is only called once in the loop, then the best (and only) strategy is to sidestep the question entirely through a trivial modification to the code: 如果你要求std::vector::size()只在循环中调用一次,那么最好的(也是唯一的)策略是完全通过对代码的微不足道的修改来回避问题:

std::vector<int> someVector;    
for (unsigned int i = 0, length = someVector.size(); i < length; ++i)
{
   // do something
}

It depends on the compiler optimization. 这取决于编译器优化。

it might Loop optimization,(if possible not mentioned something like volatile) 它可能是循环优化,(如果可能的话,没有提到类似volatile的东西)

the compiler will put datas (irrelevant) not dependent on loop outside. 编译器会将数据(不相关)放在外部不依赖于循环。

so it may generate something like 所以它可能产生类似的东西

int length = someVector.length();
for (unsigned int i = 0; i < length; i++)
{
    // do something
}

There are many compiler optimization techniques that it 'll do. 它有许多编译器优化技术。

By default in c++ there are some "pure functions" string.length() which is always optimized. 默认情况下,在c ++中有一些“纯函数” string.length()总是被优化。 i'm not sure whether vector.size belong to that. 我不确定vector.size是否属于那个。

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

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