473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

M
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
Nov 9 '06 #1
10 2092
"M" <no****@here.thankyouwrote in message
news:Nj*******************@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 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.

Nov 9 '06 #2

David T. Ashley wrote:
"M" <no****@here.thankyouwrote in message
news:Nj*******************@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.

Nov 9 '06 #3
M

"ZeldorBlat" <ze********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
David T. Ashley wrote:
>"M" <no****@here.thankyouwrote in message
news:Nj*******************@text.news.blueyonder.c o.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.
Nov 9 '06 #4
"M" <no****@here.thankyouwrote in message
news:qi*******************@text.news.blueyonder.co .uk...
>
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.
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.

Nov 9 '06 #5

M wrote:
"ZeldorBlat" <ze********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...

David T. Ashley wrote:
"M" <no****@here.thankyouwrote in message
news:Nj*******************@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.

Nov 9 '06 #6
..oO(M)
>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.
>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
Nov 9 '06 #7

M wrote:
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.

Nov 9 '06 #8
Chung Leong wrote:
<snip>
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/

Nov 10 '06 #9

R. Rajesh Jeba Anbiah wrote:
Chung Leong wrote:
<snip>
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.

Nov 10 '06 #10
Chung Leong wrote:
<snip>
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/

Nov 11 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: teedilo | last post by:
We have an application with a SQL Server 2000 back end that is fairly database intensive -- lots of fairly frequent queries, inserts, updates -- the gamut. The application does not make use of...
1
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
22
by: Michael Gorbach | last post by:
I was asked this summer to write a monte carlo code to simulation magnetic nanoparticles. For the nonphysicists, basicly it is a simulation where most of the time is taken up by looping through...
4
by: iamnader | last post by:
Hi, Has anyone done any performance analysis of handling WndProc in C#? It seems like there must be significant overhead in jumping between native code and the CLR for every single Windows...
2
by: Peter Bär | last post by:
A Question to the C#/.Net Gods of this forum: are there performance penalties when i compile (C#, FW1.1, ASP.NET, Studio2003) a central baseclass in a different assembly than all the derived...
7
by: arndt.blumenthal | last post by:
Hi, i implement a ASP.NET-Application for a customer. Every day the customer has the problem, that the first access to the application takes very long. What could be the reason? Thanks for...
6
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran...
9
by: Jon Slaughter | last post by:
I have to output a lot of html stuff mixed with php and I'm curious if its better to echo the html code or stop the php parser temporarily to got into "html mode"? Is there any drawbacks to either...
10
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.