Optimize “for” loop

后端 未结 3 1806
醉话见心
醉话见心 2021-01-13 19:31
std::vector someVector;    
for (unsigned int i = 0; i < someVector.size(); i++)
{
   // do something
}

Does the value of someV

3条回答
  •  悲哀的现实
    2021-01-13 19:55

    It depends on the compiler optimization.

    it might Loop optimization,(if possible not mentioned something like 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. i'm not sure whether vector.size belong to that.

提交回复
热议问题