In the following code:
std::vector var;
for (int i = 0; i < var.size(); i++);
Is the
as others said, The compiler shall decide what to do with the actual code written. The key figure is that it is called each time. But if you want to get a performance boost, it is best to write your code with some considerations. Your case is one of them, there are others as well, like the difference between these two pieces of code:
for (int i = 0 ; i < n ; ++i)
{
for ( int j = 0 ; j < n ; ++j)
printf("%d ", arr[i][j]);
printf("\n");
}
for (int j = 0 ; j < n ; ++j)
{
for ( int i = 0 ; i < n ; ++i)
printf("%d ", arr[i][j]);
printf("\n");
}
The difference is that the first one will not change the ram page too much per references, but the other will exhaust your cache and TLB and other stuff.
Also inline won't help that much! because the order of the calling function will remain as n(size of the vector) times. It helps in some places though, but the best thing is to rewrite your code.
But! if you want to let a compiler do it's optimizations over your code NEVER put volatile, like so:
for(volatile int i = 0 ; i < 100; ++i)
It prevents the compiler from optimizing. If you need another hint for performance use register instead of volatile.
for(register int i = 0 ; i < 100; ++i)
The compiler will try not to move i from the CPU-registers to RAM. It is not ensured that it can do it, but it will do it's best ;)