Showing posts with label large array. Show all posts
Showing posts with label large array. Show all posts

Friday, February 13, 2009

Increase Performance of Loop with Large Arrays

In PHP, this is how we usually loop through arrays:


<?php
for ($i=0; $i<count($big_array); $i++){
//
}
?>


Having this approach, program will try to count the $big_array every time it loops and it may cause some performance issues. To make it more efficient, we should code it this way:


<?php
for ($i=0, $n=count($big_array); $i<$n; $i++){
//
}
?>


It does the counting during initialization only.