Connecting Tech Pros Worldwide Help | Site Map

Performance of lots of ECHO's V Jumping In/Out of PHP

M
Guest
 
Posts: n/a
#1: Nov 9 '06
Hi,
Suppose you have the situation where you have say 20 blocks of text (~250
chars each) in a MySQL db that you need to display, but each has a condition
to check to see whether you should display it or not.

As far as I can see, you have 2 options:

1. A continuous block of php, with all the text displayed via echo commands

2. Or jumping in and out of php for each block of text i.e.
<?php ...if condition ?>
pure html
<?php ...if end ?>

Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the web
server (Apache) making repeated calls to the php engine?

My general point is, is it "better" to echo html out (css, formatting and
all) or continuously jump in/out of php (and only echo MySQL data) if you
have a lot of php logic / control in your page?

Thanks,
M


David T. Ashley
Guest
 
Posts: n/a
#2: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP


"M" <nospam@here.thankyouwrote in message
news:NjG4h.49647$r61.43856@text.news.blueyonder.co .uk...
Quote:
>
Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the web
server (Apache) making repeated calls to the php engine?
It is possible that even the authors of PHP/Zend don't know for sure.
Real-time analysis of software is complex. It might be easiest for you to
construct a test.

To eliminate the effects of network variability, you could use "curl" and
"time" directly on the remote machine with Apache and PHP.

Dave.



ZeldorBlat
Guest
 
Posts: n/a
#3: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP



David T. Ashley wrote:
Quote:
"M" <nospam@here.thankyouwrote in message
news:NjG4h.49647$r61.43856@text.news.blueyonder.co .uk...
Quote:

Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the web
server (Apache) making repeated calls to the php engine?
>
It's also worth noting that Apache only makes one call to the PHP
engine. In other words, the entire script is parsed and executed by
PHP -- there is no "jumping in and out" just because the ?tag is
encountered.

M
Guest
 
Posts: n/a
#4: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP



"ZeldorBlat" <zeldorblat@gmail.comwrote in message
news:1163082078.036743.251700@e3g2000cwe.googlegro ups.com...
Quote:
>
David T. Ashley wrote:
Quote:
>"M" <nospam@here.thankyouwrote in message
>news:NjG4h.49647$r61.43856@text.news.blueyonder.c o.uk...
Quote:
>
Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the
web
server (Apache) making repeated calls to the php engine?
>>
>
It's also worth noting that Apache only makes one call to the PHP
engine. In other words, the entire script is parsed and executed by
PHP -- there is no "jumping in and out" just because the ?tag is
encountered.
>
So in that case - PHP engine scans the whole page of text, acting only on
code it finds between the php start / end tags?
I guess then I am wondering whether it is more efficient for the PHP engine
to start / stop it's processing of php code multiple times or whether it is
more efficient to output ECHO statements between the if conditions? Think
that I'll put a test page together to test this.

As a side question / answer - what would peoples preference be in this case
from a view point of understanding / reading / maintaining code be?

M.


David T. Ashley
Guest
 
Posts: n/a
#5: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP


"M" <nospam@here.thankyouwrote in message
news:qiH4h.49673$r61.46796@text.news.blueyonder.co .uk...
Quote:
>
So in that case - PHP engine scans the whole page of text, acting only on
code it finds between the php start / end tags?
You have to understand that PHP and/or the Zend scripting engine is going to
be designed using formal lexical analysis theory (Lex, Yacc, that kind of
thing), and will be blindingly fast. You'll probably find that you can't
measure a speed difference between echo'd HTML and inline HTML. "echo" is
probably a built-in operator handled mostly by the parser (i.e. not a
function call with a formal interface like most functions). It may be
slightly slower, but any difference may be very hard to measure.

In any case, let us know what you find.
Quote:
As a side question / answer - what would peoples preference be in this
case from a view point of understanding / reading / maintaining code be?
That depends on why "echo" is used.

If it is used for invariant HTML that could just be put inline, then
definitely the inline HTML is easier for people to deal with. IMHO.



Tim Hunt
Guest
 
Posts: n/a
#6: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP



M wrote:
Quote:
"ZeldorBlat" <zeldorblat@gmail.comwrote in message
news:1163082078.036743.251700@e3g2000cwe.googlegro ups.com...
Quote:

David T. Ashley wrote:
Quote:
"M" <nospam@here.thankyouwrote in message
news:NjG4h.49647$r61.43856@text.news.blueyonder.co .uk...

Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the
web
server (Apache) making repeated calls to the php engine?
>
It's also worth noting that Apache only makes one call to the PHP
engine. In other words, the entire script is parsed and executed by
PHP -- there is no "jumping in and out" just because the ?tag is
encountered.
>
So in that case - PHP engine scans the whole page of text, acting only on
code it finds between the php start / end tags?
I guess then I am wondering whether it is more efficient for the PHP engine
to start / stop it's processing of php code multiple times or whether it is
more efficient to output ECHO statements between the if conditions? Think
that I'll put a test page together to test this.
>
As a side question / answer - what would peoples preference be in this case
from a view point of understanding / reading / maintaining code be?
>
M.
One of the php core team covers your original question in her blog,
check it out. In the comments she says the time difference is
nanoseconds

http://blog.libssh2.org/index.php?/a...of-string.html

Re: Readability/easy maintenance, feel free to use one, the other or
both, it depends on what you are doing. I've a slight preference for
inline html but when there's a more php variables to display than html
then I'll usually use echo.

Michael Fesser
Guest
 
Posts: n/a
#7: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP


..oO(M)
Quote:
>So in that case - PHP engine scans the whole page of text, acting only on
>code it finds between the php start / end tags?
Yep.
Quote:
>I guess then I am wondering whether it is more efficient for the PHP engine
>to start / stop it's processing of php code multiple times or whether it is
>more efficient to output ECHO statements between the if conditions?
Do you really care about some CPU cycles? It's like optimizing string
output with single quotes instead of double quotes, but still using a
bubble sort algo. If you get performance problems, you should run a
profiler to see where your script wastes time. I'm sure it won't be the
'echo' part. The bottlenecks are usually somewhere else.

I prefer your first version, simply because the code is more readable
and easier to maintain.

Micha
Chung Leong
Guest
 
Posts: n/a
#8: Nov 9 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP



M wrote:
Quote:
Hi,
Suppose you have the situation where you have say 20 blocks of text (~250
chars each) in a MySQL db that you need to display, but each has a condition
to check to see whether you should display it or not.
>
As far as I can see, you have 2 options:
>
1. A continuous block of php, with all the text displayed via echo commands
>
2. Or jumping in and out of php for each block of text i.e.
<?php ...if condition ?>
pure html
<?php ...if end ?>
>
Which would be more performant? I understand that echo's are poor in
performance with large blocks of text, but what is the overhead of the web
server (Apache) making repeated calls to the php engine?
>
My general point is, is it "better" to echo html out (css, formatting and
all) or continuously jump in/out of php (and only echo MySQL data) if you
have a lot of php logic / control in your page?
>
Thanks,
M
Text outside of <?php ?is compiled to echo operations. So there's no
basic difference between the two methods.

R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#9: Nov 10 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP


Chung Leong wrote:
<snip>
Quote:
Text outside of <?php ?is compiled to echo operations. So there's no
basic difference between the two methods.
If it's true, manual is contradicting:

<quote src="http://in.php.net/language.basic-syntax#AEN2651">

<snip>
} else {
?>
<strong>This is false.</strong>
<?php
}
?>

This works as expected, because when PHP hits the ?closing tags, it
simply starts outputting whatever it finds until it hits another
opening tag. The example given here is contrived, of course, but for
outputting large blocks of text, dropping out of PHP parsing mode is
generally more efficient than sending all of the text through echo() or
print().

</quote>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Chung Leong
Guest
 
Posts: n/a
#10: Nov 10 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP



R. Rajesh Jeba Anbiah wrote:
Quote:
Chung Leong wrote:
<snip>
Quote:
Text outside of <?php ?is compiled to echo operations. So there's no
basic difference between the two methods.
>
If it's true, manual is contradicting:
>
<quote src="http://in.php.net/language.basic-syntax#AEN2651">
>
<snip>
} else {
?>
<strong>This is false.</strong>
<?php
}
?>
>
This works as expected, because when PHP hits the ?closing tags, it
simply starts outputting whatever it finds until it hits another
opening tag. The example given here is contrived, of course, but for
outputting large blocks of text, dropping out of PHP parsing mode is
generally more efficient than sending all of the text through echo() or
print().
>
</quote>
It's poorly worded in the manual. Clearly, what is expected is that the
conditional would determine whether the HTML appear or not. If PHP
simply outputs anything outside of <?php ... ?>, then both blocks would
always appear. Nor would the following work as "expected."

<?php foreach($words as $word) { ?>
<li><?php echo $word; ?></li>
<?php } ?>

Think about it. The way I described it is the only way it could work.
The PHP language engine cannot control something that isn't part of the
code-stream.

Anyway, it's in the source code. If you look in zend_compile.c, you
will find this:

switch(retval) {
[...]
case T_OPEN_TAG_WITH_ECHO:
retval = T_ECHO;
break;
[...]
}

retval comes from lexical analyzer (lex). As you can see, when <?php is
encountered, an echo op-code is added--for the text that comes before.

R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#11: Nov 11 '06

re: Performance of lots of ECHO's V Jumping In/Out of PHP


Chung Leong wrote:
<snip>
Quote:
It's poorly worded in the manual. Clearly, what is expected is that the
conditional would determine whether the HTML appear or not. If PHP
simply outputs anything outside of <?php ... ?>, then both blocks would
always appear.
<snip>

Thanks for your insight.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Closed Thread