Friday 13 February 2009
Facebook StumbleUpon Twitter Google+ Pin It

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.
Parthiv Patel
Bhaishri Info Solution
Sr. PHP Developer
Limdi Chowk, AT PO. Nar, Di. Anand
Nar, Gujarat
388150
India
pparthiv2412@gmail.com
7383343029
DOB: 12/24/1986

No comments: