NYCPHP Meetup

NYPHP.org

[nycphp-talk] string function

John Campbell jcampbell1 at gmail.com
Thu Jul 3 12:06:54 EDT 2008


On Thu, Jul 3, 2008 at 11:11 AM, Urb LeJeune <urb at e-government.com> wrote:
> Is this the same thing that you are trying to accomplish with the
> for loop?
>
> Yes but increments (or decrements) are executed much more efficiently than
> additions.

This is wrong for three reasons:
1) 4 ++ increments is 2.5x slower than one increment by two.
2) Unless you are using an opcode cache, any gains from faster
execution are probably lost by the additional parsing required.  Of
course this depends of the number of elements in the loop.
3) Unreadable code is orders of magnitude more expensive than the
performance benefit.

Ignoring #3, the fastest way to write it is something like:

$p = explode('|',$y);
for($i=0,$c = count($p);$i<$c;) {
echo $p[$i++],' ', $p[$i++], "<br>\n";
}

Removing the string concatenation is more beneficial than any
increment / decrement hacking.

Regards,
John C.



More information about the talk mailing list