SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    -
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    In and out of PHP mode, which one is faster?

    I have got serious suspcious about which one is faster, let's say that we have used PHP functions everywhere in the page even for publishing standart html tags which doesn't need any server side calculations.

    Ont the other hand let's say that we always consider to get out of PHP mode in to standart html mode whenever possible to help the web server software to not read every little line to lok for interpreting PHP.

    Does the two mode conclude in a very significant speed difference? A good example could be;

    For case 1:

    echo "<a href=\"$url\" class=\"link\" target=\"_top\">$linkname</a>";

    For case 2:

    <a href="<?php echo $url; ?>" class="link" target="top"><?php echo $linkname; ?></a>

    Thanks for the upcoming answers,

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you put a for loop around the code and time which one's faster.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A simple function to help you with the test:

    PHP Code:
    <?php

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

    $time_start getmicrotime();

    // FUNCTION HERE


    //Stop the timing and print it:
    $time_end getmicrotime();
    $time $time_end $time_start;

    echo 
    "$time seconds";


    ?>
    Since the runtime you'll be getting is pretty small, put them in a while loop for 100 to 1000 times will get a more accurate result, the more times you run it the more accurate it'll be.

    Looking forward in hearing about your results.

  4. #4
    SitePoint Member
    Join Date
    May 2001
    Location
    -
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Som results

    Thanks for the detailed answer, before reading your answer, I perpared a different test environment and let it run for 20 minutes.

    First I have created a sample page which has got 21 tables and 7 other tables which contain the data gathered from mySQL. The page has got automatic reloading whenever it is loaded and each time it is loaded it updates a stat in a mwSQL table so I don't have to wait and count.

    The ALL-PHP sample page (every code is interpreted by PHP - every table are created with echo function) was able to reload 580 times in 20 minutes.

    The PHP-HTML sample page (PHP was used to gather data from SQL and only the page enters the PHP mode when it needs to publish the live data) was able to reload around 2000 times.

    As you can see it is so obvious that whenever it is possible we have to get out of PHP mode cause the web server has to read every bit of code looking for places to use its PHP capabilities if we are in the PHP mode but if we get out of PHP mode and use it when it is needed both the resources are not wasted and the interpreting of the page gets faster.

    It is also interesting to point out that there was another sample page which I created with Flash banners embedded. While I was testing with that page the results were identical both with ALL-PHP and PHP-HTML test pages because the Flash banners were overloading the system and creating a bottleneck. So one must reallay be carefull when preparing such tests,

    Thanks, will try your solution, too,

    alpyo

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow alpyo, that's amazing. i'm gonna run my own benchmarks sometime too as i'm also very curious about the same thing. (always trying to squeeze every bit of performance out of something )

  6. #6
    SitePoint Member
    Join Date
    Mar 2001
    Location
    Pezenas, South of France
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Benchmarking

    Did Dr Larry Pepper ever get to do some PHP benchmarking?
    Tony

  7. #7
    SitePoint Member
    Join Date
    Mar 2001
    Location
    Pezenas, South of France
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Benchmarking 2

    It seemed to me that the benchmark described could be influenced by a number of external factors. Ideal for real world benchmarking but not too helpful for understanding what is actually going on.

    My benchmark is simpler. It is based on the loop (shown above) described in microtime in the PHP manual. Inside the loop is a block of characters which are either comment or 19 paired closing and opening PHP tags. Both blocks are the same number of characters. So this loop either does nothing 1000 times or opens and shuts PHP 19,000 times.

    As part of its design this experiment runs 50 cycles, each of which is four inner loops - nothing, PHP, PHP, nothing.

    The experiment is run in the early morning EST (ie 0600 - 1000 GMT) on machines which I presume to be quiet. One run takes about 3 minutes.

    The nothing loops are extremely consistent in their timing, taking about 0.7 milliseconds to complete. I have seen the suggestion that a benchmark is not worthwhile unless it runs for a longer time. This particular benchmark has now been run on the same machine several hundred times and more than 85% of its repetitions lie between 733 and 743 microseconds.

    The PHP loops are extremely inconsistent and appear to comprise two kinds of result. The quicker responses are, in themselves, highly consistent and look like slower versions of the nothing loops. They time at about 10 milliseconds and sit in the range 9,790 to 10,500 microseconds. The slower responses are in the range 1.4 - 2.4 seconds with outliers of 3 - 10 seconds and one of 27 seconds.

    The 'abba' design of the loops means that the consistent nothing loops are actually being measured in amongst the inconsistent longer loops.

    Interpretation? Since all that happens is that PHP opens and closes it consists of two processes seen by Apache or Linux as having different priorities.

    The process which has to read all the characters to see if they are PHP or not is of low priority and can get knocked off easily. Hence the opportunities for delay.

    The process which interprets the PHP is of higher priority and can get knocked off with difficulty. Hence the consistent quick times. The difference between the nothing loops and the quickest PHP loops is the time it takes to execute the 19,000 open and close tags, ie about half a microsecond. The difference between the nothing loops and the slower PHP loops is the time you can have 'stolen' from your process by allowing another process to interrupt you - in my worst case the estimate would be a couple of milliseconds per pair of tags, not much until you do it 19,000 times!

    While this result is the opposite of alpyo's result, and is perhaps a less 'real world' benchmark than his, it suggests that staying in PHP would be quicker than jumping in an out, if you have no need to do that for things like database handling.

    And, I guess, it particularly suggests staying in PHP inside loops if you can manage that.

    I would love to hear comments
    Last edited by tuppence; Jul 8, 2001 at 02:29.
    Tony

  8. #8
    SitePoint Enthusiast Ckeren's Avatar
    Join Date
    Jun 2001
    Location
    AU
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That was Cool !

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: PHP Benchmarking

    Originally posted by tuppence
    Did Dr Larry Pepper ever get to do some PHP benchmarking?
    no, not yet...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •