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

begin:vcard
fn:Kenneth  Downs
n:Downs;Kenneth 
adr;dom:;;347 Main Street;East Setauket;NY;11733
email;internet:[EMAIL PROTECTED]
tel;work:631-689-7200
tel;fax:631-689-0527
tel;cell:631-379-0010
x-mozilla-html:FALSE
url:http://www.secdat.com
version:2.1
end:vcard

_______________________________________________
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

Reply via email to