-1

What is more efficient for the following loop operation in PHP:

$k = count($array);
for ($i = 0;  $i < $k; $i++)
{
    echo $array[$i];
}

Or

for ($i = 0;  $i < count($array); $i++)
{
    echo $array[$i];
}

I know that in JavaScript there is something like this possible and more efficient:

for (i = 0, j = array.length; i < j; i++)
{
    console.log(array[i]);
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Tudor Ravoiu
  • 2,130
  • 8
  • 35
  • 56
  • This question is just academic, in practice you won't notice a difference, especially if you are not changing the size of the array while iterating. – akirk Mar 28 '14 at 08:44
  • 1
    Your first example is the best way to do a for loop since you've cached the value of `$k`, in your second example you're recounting after every iteration. – James Mar 28 '14 at 08:44
  • Also, count is O(1): http://stackoverflow.com/questions/5835241/phps-count-o1-or-on-for-arrays – akirk Mar 28 '14 at 08:47
  • You can use the same syntax in PHP as well: `for ($i = 0, $j = count($array); $i < $j; $i++)` – Gumbo Mar 28 '14 at 09:10

7 Answers7

4

This one is more efficient than 2nd one

<?php 
$k = count($array);
for($i = 0;  $i < $k; $i++)
{
    echo $array[$i];
}
?>

thanks.

  • 1
    Some explanation as to why it is more efficient would help.... – Tim B Mar 28 '14 at 09:04
  • because when you debug the code then you can get idea that each time system need to count the total number of element from the array so better you use the one variable and pass the total number of array's element so loop no need to count array element each time... – anant prajapati Mar 29 '14 at 08:21
3

In this specific case your first example is faster, simply because to evaluate the condition $i < count($array) PHP will execute count() after every loop. So following that logic it would be faster to use a variable in your condition, rather than a function.

The more practical approach would be to use foreach() as mentioned elsewhere.

Dragony
  • 1,712
  • 11
  • 20
2

The first one is more efficient, the second one is calling the function count() each iteration.

naoxink
  • 595
  • 1
  • 12
  • 19
1

I would use a foreach in this context.

From the PHP Docs..

The foreach construct provides an easy way to iterate over arrays.

Why foreach is efficient in this case ?

  • Definitely faster since you won't be calling a count() for determining the length of the array as you did above.

  • You can even access the keys of the arrays.

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • Also see this Stackoverflow question: http://stackoverflow.com/questions/3430194/performance-of-for-vs-foreach-in-php – akirk Mar 28 '14 at 08:43
  • 3
    Ease of use often comes with additional computations behind the curtain. – Gumbo Mar 28 '14 at 09:14
1

Lets test and see.

 $array = array();
 $array = array_pad($array, 1000, 0);
 $iters = 1e4;

 $tic = microtime(true);
 for ($jj = 0; $jj < $iters; $jj++) {
   $k = count($array);
   for($i = 0;  $i < $k; $i++) {
       $vv = $array[$i];
   }
 }
 $toc = microtime(true);
 $time = $toc - $tic;
 echo "Time for external \$k..: $time\n";


 $tic = microtime(true);
 for ($jj = 0; $jj < $iters; $jj++) {
   for($i = 0;  $i < count($array); $i++) {
       $vv = $array[$i];
   }
 }
 $toc = microtime(true);
 $time = $toc - $tic;
 echo "Time for count(\$k)....: $time\n";



 $tic = microtime(true);
 for ($jj = 0; $jj < $iters; $jj++) {
   foreach ($array as $i => $v) {
       $vv = $array[$i];
   }
 }
 $toc = microtime(true);
 $time = $toc - $tic;
 echo "Time foreach..........: $time\n";

Results:

 Time for external $k..: 5.2356481552124
 Time for count($k)....: 35.91916513443
 Time foreach..........: 4.0548861026764
Victory
  • 5,811
  • 2
  • 26
  • 45
0

I think the following would be more efficient

for ($i = 0;  $i < count($array); $i++)
{
    echo $array[$i];
}

as no extra variable is defined to store the count of array requiring less memory space

aΨVaN
  • 1,104
  • 2
  • 9
  • 18
  • Quote James: "Your first example is the best way to do a for loop since you've cached the value of $k, in your second example you're recounting after every iteration" – kevinoo Mar 28 '14 at 08:51
-1
<?php 

    foreach ($yourArayValue as $test=>$value)
      {
      echo $test;
      }
?>

foreach() is batter

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
  • what the reason of down-vote :( – Ferrakkem Bhuiyan Mar 28 '14 at 08:55
  • I didn't downvote but the reasons look obvious, The suggestion you gave is not even the one that was asked. The original question didn't ask for a foreach and everyone is answering that, secondly just saying something is better doesn't make it better. You have to give reasons for your claim. – Hanky Panky Mar 28 '14 at 09:03