1

Is there a way to make this faster?

while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>', "\n";
    next($data);
}

I do not like that I need to create new variables like $item.

Alex L
  • 8,419
  • 6
  • 43
  • 51
  • 4
    You don't need the $item declaration at all, actually. – Ian Elliott Jul 21 '09 at 01:56
  • Wouldn't you just have to put $item = current($data) in the while instead of the statement? – Tyler Carter Jul 21 '09 at 02:00
  • I checked using microtime for speed. And it seems that using "while (current($data))" is slower than "while ($item = current($data))" but very little. Using foreach is significantly faster. – Alex L Jul 21 '09 at 02:27
  • While foreach is faster, it does create variables. The whole attempt to avoid creating variables is misguided (though commmon in the PHP world). Check my remix of your question here http://stackoverflow.com/questions/1156936/how-to-make-array-loop-faster-in-php – Dan Rosenstark Jul 21 '09 at 10:20
  • @yar, you linked to this page – Alex L Jul 21 '09 at 20:22
  • Also, I don't think creating variables will necessarily slow down the script. However, in case I have big data blocks, I would not want to waste memory but duplicating it. – Alex L Jul 21 '09 at 21:09

7 Answers7

4
<?php
$transport = array('foot', 'bike', 'car', 'plane');

foreach ($transport as $value) {
    echo $value;
}
?>
Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
1

If you don't want to create temporary variables, do it like this:

while (current($data))
{
    echo '<ATTR>',current($data), '</ATTR>', "\n";
    next($data);
}

However, I don't know if this will really make it any faster. They only way to tell would be with a profiler, but it is such a micro-optimization I doubt you will notice the difference.

The best way to speed up the loop would be to use a faster computer.

a_m0d
  • 12,034
  • 15
  • 57
  • 79
  • Agreed, and if the loop is big enough for there to be a noticeable difference then the time it takes for the server to send back the response will dwarf the time saved optimizing it. – Kane Wallmann Jul 21 '09 at 02:06
  • I checked for speed using microtime() and foreach is faster than current() and next() looping. – Alex L Jul 21 '09 at 02:25
  • Yes, but you said that you don't want to create those temporary variables - with the `foreach` loop you are creating 2 of them! – a_m0d Jul 21 '09 at 02:39
1

If all you're doing is the code above you could use an implode statement.


if (count($data) > 0) {
     echo "<ATTR>".implode("</ATTR>\n<ATTR>", $data)."</ATTR>";
}
rezzif
  • 460
  • 2
  • 10
1

$nl = "\n";

while ($item = current($data))
{
    echo '<ATTR>',$item, '</ATTR>',$nl;
    next($data);
}

Store the newline character into a variable rather then having PHP parse the double quotation marks in every iteration.

Kane Wallmann
  • 2,292
  • 15
  • 10
1

I do a little bench to comprobe it.

<?php

$a = array();
for ($i = 0; $i < 100000; $i++) {
    $a[] = $i;
}

$start = microtime(true);
foreach ($a as $k => $v) {
    $a[$k] = $a[$k] + 1;
}
echo "foreach               : ", microtime(true) - $start, " Seconds\n";

$start = microtime(true);
foreach ($a as $k => &$v) {
    $v = $v + 1;
}
echo "foreach with cursor   : ", microtime(true) - $start, " Seconds\n";

$start = microtime(true);
for ($i = 0; $i < count($a); ++$i) {
    $a[$i] = $a[$i] + 1;
}
echo "for                   : ", microtime(true) - $start, " Seconds\n";

$start = microtime(true);
for ($i=0,$l=count($a);$i<$l;++$i) {
    $a[$i] = $a[$i] + 1;
}
echo "for with cached count : ", microtime(true) - $start, " Seconds\n";

With results

foreach               : 0.0039410591125488 Seconds
foreach with cursor   : 0.00357985496521 Seconds
for                   : 0.0022602081298828 Seconds
for with cached count : 0.0020480155944824 Seconds

Hope this helps

Felipe Buccioni
  • 19,109
  • 2
  • 28
  • 28
0

You could do a foreach, but then you would be creating 2 new variables. Unless you just don't like the idea of assigning variables inside the while() clause.

foreach($data as $key => $value)
{
    echo $key . " => ".$value;
}

Either way, you are going to need to create an actual variable.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
0

What about this one :

function my_func($str) {
    echo "<attr>{$str}</attr>\n";
}
array_map('my_func', $data);

(Should work, but I'm curious about it's speed compared with a foreach loop)

Or, if you are using PHP >= 5.3 (probably not your case, btw), you can use this one, based on a lambda function :

array_map(function ($item) {
    echo "<attr>{$item}</attr>\n";
}, $data);

Almost the same, but without having to declare a function used only once in the program.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663