tags:

views:

343

answers:

6

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.

A: 

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.

Chacha102
+2  A: 
<?php
$transport = array('foot', 'bike', 'car', 'plane');

foreach ($transport as $value) {
    echo $value;
}
?>
Luca Matteis
using foreach made it faster
Alex L
+1  A: 

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
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
I checked for speed using microtime() and foreach is faster than current() and next() looping.
Alex L
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
+1  A: 

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
You mean implode( "\n", $data );
Kane Wallmann
Which, when processed, will probably take the same amount of (micro)time, since it probably does a foreach loop.
Chacha102
Ahh he edited his post...
Kane Wallmann
Thanks, I checked this and it's faster than what I had, but using foreach seems just a bit faster.
Alex L
Yeah i'm not really sure how much quicker it would be - seems a bit like micro optimisation to me :P
rezzif
+1  A: 

Hey try this

$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
That's a good tip, thanks.
Alex L
Alex. Try using PHP_EOL instead of storing \n as a variable.
hobodave
A: 

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