In an infinite loop, i want to break out based on number of elements in an array. Say:
$myarr = array();
While (True){
//... do something that modifies $myarr ...
if (count($myarr) > 100000) { break; }
}
The problem is, every time i try to code this way, thoughts of micro-optimization creeps in my mind(blame me). I tell myself: why not just use a variable to keep track of the number of elements in the array? Like this:
$myarr = array();
$n_myarr = 0;
while (True){
// ... do something that modifies $myarr
if ( ... elements added ... )
{ $n_myarr += $n_elements_added; }
else if ( ... elements removed ... )
{ $n_myarr -= $n_elements_removed; }
if ($n_myarr > 1000000) { break; }
}
As far as I understand, how count() performs is completely dependent on underlying implementation of count() and array. I always prefer to write in simpler ways, if i can, like the 1st code snippet. Can anyone enlighten me on this subject? Especially, how does count() work under the hood?
Thank you.
-Titon