Forum Moderators: coopster

Message Too Old, No Replies

PHP printing / echoing performance question

         

ProFiler

10:22 pm on Jan 27, 2004 (gmt 0)

10+ Year Member



Something I've always been wondering, what method is faster faster? (see A through D below)
I canīt even imagine how many times I've wondered this in the past years. I know the speed differences would be minimal with code as short as the stuff below.

Anyone have any info or documentation on this?

A)

<style type="text/css">
<!--
.filebrowser_head{font-size:16px; font-family:arial; color:<? print $css_filebrowser_head;?>; text-decoration:underline;}
A.filebrowser_head:link{font-size:16px; font-family:arial; color:<? print $css_filebrowser_head;?>; text-decoration:underline;}
-->
</style>

or B)

<?
print '<style type="text/css">
<!--
.filebrowser_head{font-size:16px; font-family:arial; color:$css_filebrowser_head; text-decoration:underline;}
A.filebrowser_head:link{font-size:16px; font-family:arial; color:$css_filebrowser_head; text-decoration:underline;}
-->
</style>';
?>

or C)

<?
print "<style type="text/css">\n";
print "<!--\n";
print ".filebrowser_head{font-size:16px; font-family:arial; color:$css_filebrowser_head; text-decoration:underline;}\n";
print "A.filebrowser_head:link{font-size:16px; font-family:arial; color:$css_filebrowser_head; text-decoration:underline;}\n";
print "-->\n";
print "</style>\n";
?>

or even D)

<?
print "<style type="text/css">\n";
print "<!--\n";
print ".filebrowser_head{font-size:16px; font-family:arial; color:".$css_filebrowser_head."; text-decoration:underline;}\n";
print "A.filebrowser_head:link{font-size:16px; font-family:arial; color:".$css_filebrowser_head."; text-decoration:underline;}\n";
print "-->\n";
print "</style>\n";
?>

coopster

1:43 pm on Jan 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>Anyone have any info or documentation on this?

You bet! You can test it using the methods described in PHP code performance testing [webmasterworld.com].

ergophobe

5:42 pm on Jan 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ProFiler,

With that handle, I'd expect you would already have the full statistical workup on this ;-)

As coopster says, the only way to know is to benchmark it. Do many iterations as you will be using microtime() which is not super accurate as a benchmarking tool. If you do something like 100 iterations on a really quick script like that, your standard error will likely be about as high as the difference between the various versions. You'll likely need to do something like 1-10K iterations.

Anyway, I have benchmarked these and some other output methods such as
-heredoc
-identical except for single/double quote (which actually makes a tiny difference, but so tiny that I can't remember which is faster).

If I recall (*** warning: potential misinformation follows as memory fails)...

1. First principle - compared to most things in a script (i.e. loops with database queries in them), this stuff just doesn't matter and is probably the last stuff you should optimize.

2. That said there are some differences, most of them insignificant. The general rules I found though were:

- if you have say 500 words of text and you will be switching in and out PHP 50 times using method A, it will be way faster if you echo everything and use variable substitution. If you have a block of several hundred words without any PHP processing, you will gain some performance by switching out. In essence, it is quicker to just serve up text without sending it through the PHP engine, but there is some overhead every time you invoke it and you need to run a lot of text before you balance out the hit by leaving it and coming back.

- It is slightly slower to use variable substition than explicit concatenation.

If you benchmark it, I can tell you which will be the fastest - method B. Only one problem - it will not output the same as the other methods because there is no var substitution within single quotes.

So it will be super fast, it just has the minor drawback that it won't work!

Of the methods that will actually work, I think D will be fastest - doesn't switch in and out of PHP frequently and uses concatenation not substitution.

Just FYI, having benchmarked them, my conclusion was to use the method that is easiest to code and read and focus on other optimizations.

If instead of PHP benchmarking, you benchmark the time to serve the page using Apache Bench (more accurate really), I think you find that getting rid of newlines and whitespace would add more overall efficiency than choosing between methods C and D.

Tom