Speed of different approaches to php coding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beary
    New Member
    • Nov 2006
    • 170

    Speed of different approaches to php coding

    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,
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    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.

    Comment

    • beary
      New Member
      • Nov 2006
      • 170

      #3
      Originally posted by hsriat
      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.

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        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!

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          [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=\"backgro und-color:red;\">Wi th 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=\"backgro und-color:red;\">Wi thout 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?
          Code:
          With echo: 710721
          Without echo: 510907

          Comment

          • coolsti
            Contributor
            • Mar 2008
            • 310

            #6
            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.

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              My server is giving slow results:
              Code:
              With echo: 39160728
              Without echo: 38643746
              But almost the same. Were you using your PC or server?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Where is appliable: keep your front-end (html..) seperate from your back-end (php).

                Things get complicated otherwise.

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  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).

                  Comment

                  • TheServant
                    Recognized Expert Top Contributor
                    • Feb 2008
                    • 1168

                    #10
                    Originally posted by pbmods
                    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!

                    Comment

                    • beary
                      New Member
                      • Nov 2006
                      • 170

                      #11
                      Originally posted by pbmods
                      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...

                      Comment

                      • coolsti
                        Contributor
                        • Mar 2008
                        • 310

                        #12
                        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!

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          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:
                            [code=php]
                            if( ! $variable )
                            {
                            doSomething();
                            }
                            [/code]

                            However, on a couple of systems (so far, I think the common culprit is PHP 5.1), this method is actually faster:
                            [code=php]
                            if( $variable )
                            {
                            }
                            else
                            {
                            doSomething();
                            }
                            [/code]

                            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).

                          Comment

                          • perhapscwk
                            New Member
                            • Sep 2007
                            • 123

                            #14
                            Originally posted by beary
                            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.

                            Comment

                            • dlite922
                              Recognized Expert Top Contributor
                              • Dec 2007
                              • 1586

                              #15
                              Originally posted by beary
                              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.

                              Comment

                              Working...