473,382 Members | 1,329 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,382 software developers and data experts.

Speed of different approaches to php coding

170 100+
Hi all,

Wondering if someone knew which is normally quicker:

1) Writing mostly html code and inserting <?php ... ?> when required.

2) Writing mostly <?php ?> and using echo to print stuff, even when it's mostly html.

And is there some way I can test this for myself?

Thanks,
May 28 '08 #1
14 2282
hsriat
1,654 Expert 1GB
I never cared for this. Just a lazy programmer I am. If <?php tag is started, I continue using echo to print until I really need to close the php tag.
If formatting is looking good outside the <?php ?> tags, I give small one variable echos.
May 28 '08 #2
beary
170 100+
I never cared for this. Just a lazy programmer I am. If <?php tag is started, I continue using echo to print until I really need to close the php tag.
If formatting is looking good outside the <?php ?> tags, I give small one variable echos.
Yes I know what you mean. 6 of one and half a dozen of the other. But I thought maybe there was some php function maybe which might enable me to compare 2 pages written using the different methods.
May 28 '08 #3
TheServant
1,168 Expert 1GB
If you have a large amount of echo statements it can slow things down, but in most cases you will never be able to tell the difference. For a large echo statement to show an effect, it has to be really *huge*!

If you want to test it, and I would be interested:

Make two identical pages with lots of html.
Around one put:
[PHP]<?php echo <<<END
{html goes here}
END;[/PHP]

That will test how much a big statement will slow it down. To test a lot of small echos just put tonnes of echo(" {html} ") things around everything.

Two things to remember when timing. Load the page atleast once before you take a reading because there might be CSS or something to download. I use FireBug addon to my FireFox, and that has a timer for page loading. The second thing is get atleast 10 readings and average them because the load time will vary.

I'm really interested if you get it working, so let us know.
To the OP though, you shouldn't see much difference unless you have a really big page!
May 28 '08 #4
hsriat
1,654 Expert 1GB
[php]<?php
$s = whats_up();
echo "<div style=\"display:none;\">";
for ($i=0;$i<100000; $i++)
echo "this is line number 'xxx' ";
echo "</div><div style=\"background-color:red;\">With echo: ".(whats_up() - $s)."</div>";

$s = whats_up();
echo "<div style=\"display:none;\">";
for ($i=0;$i<100000; $i++)
{
?>this is line number 'xxx' <?php
}
echo "</div><div style=\"background-color:red;\">Without echo: ".(whats_up() - $s)."</div>";

function whats_up()
{
$t = gettimeofday();
return ($t['sec']*1000000) + $t['usec'];
}
?>[/php]

My machine is giving weird results.. what about yours?
Expand|Select|Wrap|Line Numbers
  1. With echo: 710721
  2. Without echo: 510907
May 28 '08 #5
coolsti
310 100+
I now use exclusively echo statements as opposed to PHP start and stop tags to allow a direct passthru of HTML code. I like this better because it makes my scripts far more readable, at least to me. Everything is PHP script.

However, since I now have just about all of the output to the user's browser built up with classes, in effect I am building up and storing all of the output either as strings in simple variables or as strings in member variables of my classes that build up the table structure of the page. So where others may use an echo statement, I have an assignment to, for example, a variable in a "TD" object which, when its print function is called will echo out the variable's value between TD tags.

This means I have the overhead of larger memory usage (to build up and store all the output before it is sent to the browser at one time) and whatever overhead is caused by repeated echo statements. I don't notice any problems with speed or efficiency, but this is a company internal application with not so many users at a time.
May 28 '08 #6
TheServant
1,168 Expert 1GB
My server is giving slow results:
Expand|Select|Wrap|Line Numbers
  1. With echo: 39160728
  2. Without echo: 38643746
But almost the same. Were you using your PC or server?
May 28 '08 #7
Markus
6,050 Expert 4TB
Where is appliable: keep your front-end (html..) seperate from your back-end (php).

Things get complicated otherwise.
May 28 '08 #8
pbmods
5,821 Expert 4TB
Heya, Beary.

You might find this article to be of some interest.

I can't find the reference at the moment, but I remember reading that keeping your HTML outside your <?php ... ?> tags is best for speed as long as you don't have too many transitions (i.e., as long as you don't put too many '<?php's in there).
May 29 '08 #9
TheServant
1,168 Expert 1GB
Heya, Beary.

You might find this article to be of some interest.

I can't find the reference at the moment, but I remember reading that keeping your HTML outside your <?php ... ?> tags is best for speed as long as you don't have too many transitions (i.e., as long as you don't put too many '<?php's in there).
I guess the real question is how many transitions is too many? Every code will be different, but I wish we could quantify it somehow!

And very good link!
May 29 '08 #10
beary
170 100+
Heya, Beary.

You might find this article to be of some interest.

I can't find the reference at the moment, but I remember reading that keeping your HTML outside your <?php ... ?> tags is best for speed as long as you don't have too many transitions (i.e., as long as you don't put too many '<?php's in there).
Hi pbmods, That site is gold! Thanks heaps for the link. Cheers...
May 29 '08 #11
coolsti
310 100+
Yes, pbmods, thanks for posting that link! A link like that is worth its weight in gold (heh, don't think it weighs too much, but you know what I mean).

I will certainly read it (and cry over how inefficient I probably have been coding over the past years) as soon as I can!
May 29 '08 #12
pbmods
5,821 Expert 4TB
It should also be noted that certain coding styles may have different effects on the speed of execution depending on variables such as:
  • PHP version and dependent software (such as Apache, MySQL, etc.).

    On some installations, this is the fastest way to check for ! $variable:
    Expand|Select|Wrap|Line Numbers
    1. if( ! $variable )
    2. {
    3.     doSomething();
    4. }
    5.  
    However, on a couple of systems (so far, I think the common culprit is PHP 5.1), this method is actually faster:
    Expand|Select|Wrap|Line Numbers
    1. if( $variable )
    2. {
    3. }
    4. else
    5. {
    6.     doSomething();
    7. }
    8.  
    The difference is appoximately .05 requests per second. I promise you, your server won't hate you either way (though the latter does increase the learning curve).

    You CAN do your own benchmarking to determine the fastest ways to perform certain operations, but given that this is generally taking advantage of version-specific quirks, you will ultimately find that you are spending a tremendous amount of time for what amounts to a very small (and - unless you plan on never upgrading your software - temporary!) increase in efficiency.
  • Zend Optimizer

    Did you know that pre-incrementing (++$i) is on average 10% faster than post-incrementing ($i++)?

    Did you also know that if you have Zend Optimizer installed on your system, it doesn't matter which method you use?

    9 times out of 10, Zend Optimizer will use pre-incrementing, even if you post-increment in your code (that 10th out of 10 is where changing to pre-incrementing would actually change the behavior of the code).

    Incidentally, Zend Optimzer is free, and it installs within minutes.

    Get Zend Optimizer
  • In certain cases, the hardware on the server itself may determine how well your code executes. Sometimes, code that is tuned for speed actually runs slower above a certain load threshold!

    This especially pertains to larger sites that have a steady and heavy User load.

    John Lim has published a fantastic article covering exactly this topic:
    A Howto on Optimizing PHP

    Additionally, there are many ways to write your code, configure Apache and organize your distribution network to minimize the *amount* of code that has to be run.

    Yahoo! has released an excellent tool for measuring your site's performance. It's called YSlow, and it integrates with FireBug, which I'm sure you already have installed (:

But a more important factor to consider (especially when developing code commercially) is not how fast the server can perform, but how quickly you can code.

If I were to spend an hour converting all of my company's PHP scripts to use single quotes instead of double quotes (where applicable), I could increase our server capacity by approximately 2 or 3 requests per second.

Out of about one million per day.

That hour could be much more productively spent working on just about anything else.

Now, there are some efficiency changes worth implementing, don't get me wrong. But before going in and changing code that already works, consider the potential costs (including the inevitable typos and logic bugs).
May 30 '08 #13
perhapscwk
123 100+
Hi all,

Wondering if someone knew which is normally quicker:

1) Writing mostly html code and inserting <?php ... ?> when required.

2) Writing mostly <?php ?> and using echo to print stuff, even when it's mostly html.

And is there some way I can test this for myself?

Thanks,
the first one is faster and html code can directly show on screen but if you put all in php, it need to send everything to server and server return the result. slower.
May 30 '08 #14
dlite922
1,584 Expert 1GB
Hi all,

Wondering if someone knew which is normally quicker:

1) Writing mostly html code and inserting <?php ... ?> when required.

2) Writing mostly <?php ?> and using echo to print stuff, even when it's mostly html.

And is there some way I can test this for myself?

Thanks,

Just use Smarty.net

Keeps your code clean, and your HTML even cleaner.
May 31 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Maboroshi | last post by:
Hi I am fairly new to programming but not as such that I am a total beginner From what I understand C and C++ are faster languages than Python. Is this because of Pythons ability to operate on...
7
by: YAZ | last post by:
Hello, I have a dll which do some number crunching. Performances (execution speed) are very important in my application. I use VC6 to compile the DLL. A friend of mine told me that in Visual...
3
by: Mike Kelly | last post by:
Hi. I've built a page using standard ASP.NET 2.0 features and when I upload a large file (>20MB) to our intranet server, I get a paltry 100KB/s on our 100Mb/s LAN. Simply copying the file, I get...
11
by: Jim Lewis | last post by:
Has anyone found a good link on exactly how to speed up code using pyrex? I found various info but the focus is usually not on code speedup.
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
11
by: Schraalhans Keukenmeester | last post by:
I have a substantial random stream of (numerical, long int) data (coming from several SCADA sources) that needs to be passed thru a function that tallies the occurrences of several different bit...
9
by: sturnfie | last post by:
Hi all, I am attempting to write a tool that would get (via something of a stopwatch action) the amount of time it takes to copy a file from a local disk to a connected USB drive on a Windows XP...
8
by: mast2as | last post by:
I am sure this topic has been discussed a thousand times and I read a few things about it today on the net. I also want to say I am trying to start a polemic here, I am just curious and willint to...
12
by: Tyno Gendo | last post by:
Hi everyone I wondered what if any methods people here use to speed up their PHP code (ie. speed at which you produce your code). Things like 'code templates', 'base objects' for this and...
4
by: Jason Huang | last post by:
Hi, I am thinking in comparing developing speed between C# Windows Form and C# Web Form applications. Assuming the scenario is that we have 2 C# coding engineers, engineer A has 2 years...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.