NYCPHP Meetup

NYPHP.org

[nycphp-talk] testing a theory

Kenneth Downs ken at secdat.com
Tue Feb 6 19:35:21 EST 2007


Adrian Noland wrote:
> Hi, I have a quick (and probably silly) question. Is there performance
> difference between the following two snippets. Does it make a
> difference if there are hundreds+ of lines?

I can offer a few rules of thumb from experience.

First, in any situation where there is disk or db access, you can assume 
that the disk access is far far more expensive than the in-memory 
operations like string assignment.  This difference is so staggering 
that all optimization must go into disk reads first.  No queries inside 
of loops, stuff like that.

Now, that being said, and assuming perfect optimization of disk reads, 
what about the snippets?  Well it would probably be nigh-on impossible 
to determine a difference in the snippets as written, but one telltale 
is that the first snippet is handling each string twice, first in the 
assignment, then in the output.  If PHP copies a string during string 
appends, then the first line is actually being handled 4 times!  This 
means we would reject it, because if you got in the habit of doing that, 
you may not notice if one day you start building strings out of much 
larger pieces, like file fragments, or big db text fields, and suddenly 
instead of 100 bytes its 100k bytes, and then wham, you do have a problem.


>
> <?php
> $content = "this is the start of a bunch of lines";
> $content .= "another line";
> $content .= "yet another";
> echo $content;
> ?>
>
> vs.
>
> <?php
> echo "this is the start of a bunch of lines";
> echo "another line";
> echo "yet another";
> ?>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ken.vcf
Type: text/x-vcard
Size: 261 bytes
Desc: not available
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070206/cd12f35e/attachment.vcf>


More information about the talk mailing list