Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output Performance Question!? 2

Status
Not open for further replies.

Forri

Programmer
Joined
Oct 29, 2003
Messages
479
Location
MT
Hi all

What do you think is best:

a) to echo each line you have to output or

b) to create a variable to store one whole string then echo at the end of the file?

Any toughts and explanation?

Thanks
Nick
 
Assuming that output buffering is not involved, I think, but cannot state for fact, that incremental print statements will be quicker.

To compile and entire HTML output into a variable requires lots of memory and processor resources to store. The system may have to dump the output into drive-based virtual memory, which can really slow down throughput. If you have multiple instantiations of that same script running, all compiling complex HTML pages into memory, the server is going to have to start doing a lot of virtual memory paging.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
So what's better?

if(isset($error))
{
$s="<strong>";
$cs="</strong>";
}

<?PHP print $s;?>Error<?PHP print $cs;?>

or

<?PHP print $s."Error".$cs;?>



 
It is better, in terms of performance, to not switch contexts between throughput context and interpreted mode.

Here's my test script:
Code:
<?php

function getmicrotime()
{ 
   list($usec, $sec) = explode(" ", microtime()); 
   return ((float)$usec + (float)$sec); 
} 

$time_start = getmicrotime();
   
for ($i=0; $i < 100000; $i++)
{
	[blue]print $i."\n";[/blue]
}

$time_end = getmicrotime();
$time = $time_end - $time_start;

echo "Did it in $time seconds\n";

I ran it in two versions. One as listed above and another with the blue section replaced by:

Code:
print $i;
?>

<?

The first version stays in interpreted mode. The second version switches to throughput mode to output the return then switches back to interpreted mode.

I ran each version of the script 50 times, 25 times from the console, 25 times through a web server.

On my LAMP (RH9/2.0.49/4.0.17/4.3.6) box, I get the following results:

[tt]Average runtimes (all times in seconds)
From the console:
context-switching version: 4.83
interpreted-mode-only version: 3.34

Through the web server:
context-switching version: 1.88
interpreted-mode-only version: 1.23[/tt]

Both through from the command-line and through the web server, switching contexts seems to slow PHP.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's what I thought but never really delved into it.

I see code that switches context and I've always wondered if that was better or not.

From your outputs, 'print'ing HTML is better than embedding PHP in HTML in a PHP page especially if you have a heavily dynamic page.


Thanks.
 
Yes. That and the fact that, at least for me, context-switching code is more difficult to read and thus less maintainable.

Keep in mind that PHP supports the heredoc syntax, too, when you want to take a large string and assign it to a variable or output it:

Code:
<?php
$foo = array ('one' => 'bar');
$bar = <<<EOSTRING
This is a heredoc string
EOSTRING;

print <<<EOOUTPUT
<html><body>
This is some output.<br>
$bar<br>
This is a value: {$foo['one']}
</body></html>
EOOUTPUT;
?>

This way, you can have large blocks of HTML preformatted in your code and still have variable interpolation, yet not have to keep track of whether your script is in throughput or interpreted mode.

Notice the curly-braces around the associative array reference in the print statement -- omit them, and you'll get a "unexpected T_ENCAPSED_AND_WHITESPACE" parse error.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's awesome!

Thanks. I wish I would have know that a couple of weeks ago.
 
Thanks sleipnir cleared a few things!

Thanks again
Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top