PHP for loop vs C for loop

Viewed 471

It is known that to do following in PHP is bad idea (because count ($array) will be called on every iteration which can seriously slow down script execution):

<?php
for ( $i = 0; $i < count ($array); ++$i )
{
   // Code here;
}

Instead one should calculate condition outside of the loop:

<?php
$a = count ($array);

for ( $i = 0; $i < $a; ++$i )
{
   // Code here;
}

I am new to compiled languages so I got a question:

Does the same rule applies to compiled languages like C and C++ for example?

Let's say I want to iterate vector in C++. Should I avoid such for loop or it is fine?

for ( int i = 0; i < vector.size(); ++i )
{
    // Code here
}

If this is not an issue in compiled languages - is it because compiler takes care for this and optimizes executable file or there is another reason behind that?

3 Answers
Related