Does the compiler optimize the second condition in the for statement?

I want to iterate over a vector by index, then I:

for(size_t i=0;i<v.size();++i)
{...}

I know that using an iterator to visit is more efficient, but I just want to discuss the compiler optimization at a technical point.

My question is: "v.size ()" is executed only once or several times?

Or the compiler will help me decide whether to calculate the size once enough or to call it several times using any optimization strategy, so I don't need:

size_t s=v.size()
for(size_t i=0;i<s;++i)

to make the code more efficient?

+4
source share
1 answer

My question is that this "v.size ()" is executed only once or several times?

, , , , .

, size() , .

, v , , , . , ( , ) , , - , , .

, , , , 1 2 , . size() is O(1), , - . .

+4

Source: https://habr.com/ru/post/1662713/


All Articles