SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Accurate Speed Measurement of execution

    Hi all,

    I'm new to this big place, so please be gentle.
    I know some basic PHP code and I'm learning more day by day by reading the forums and the Sitepoint Articles.
    But I ran into a question which I can not find the answer to.

    Sometimes you see people say that it is better to echo multiple strings with , instead of concatinatiing them with . It will be faster!
    Or some articles on the net mention that one architecture is faster than the other. Now I would like to do some tests of my own in this area.
    For example, which is faster:
    - Complex Reg Expr in one preg_match
    - multiple if and else with simple strpos

    I used the following code to measure load time:

    PHP Code:

    $time 
    explode(' 'microtime());
    $start $time[1] + $time[0];

    /* function in a loop */

    $time explode(' 'microtime());
    $finish $time[1] + $time[0];
    $totaltime = ($finish $start);

    echo 
    'This page took '$totaltime' seconds to load.<br/><br />'


    Problem:

    Every time I load the same php script I get a different load time.
    And it differes alot, sometimes up to 50% regardless if I empty my cache or not.
    So if I get that big difference in the same script, how can I compare it to an other script to see which method of coding is faster?


    Thanks to everybody who wants to help me!
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    $time_start = microtime(true);
    
    /* your function to measure */
    
    $time_end = microtime(true);
    $total = $time_end - $time_start;

  3. #3
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your answer Sharedlog

    But after testing it gave the same problem.
    And reading about it in the manuall, it seems that the TRUE flag just means to return a floating point and not a string seperated by a space.
    So in theory that is the same code, but a shorter version.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Run the test many times to gather an average execution time. There are many factors which at any given point in time might result in a slower execution of any piece of code.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fristi View Post
    Thank you for your answer Sharedlog

    But after testing it gave the same problem.
    And reading about it in the manuall, it seems that the TRUE flag just means to return a floating point and not a string seperated by a space.
    So in theory that is the same code, but a shorter version.
    Well, its not unusual to have different execution time every time you execute the same script. It depends on many factors like server load, available memory at the moment or execution and also OS will probably read the php file from the hard drive much faster the second time.

    Also if you function involves a database access, that's a whole different story.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know why, but the first time you call microtime() in a script it introduces a lot of variance.

    php is a managed language, where it handles memory allocation and garbage collection and many other things for you behind the scenes, when it deems suitable. This will mess with your ability to profile execution speed.

    Like said by the others, you need to loop the operation many times to help get an average that is less affected by these external factors you cannot control.

    PHP Code:
    // always ignore the first value this function returns
    microtime(true);

    // this time its more consisntant
    $time_start microtime(true);

    for (
    $i=0$i<10000$i++) {
        
    // do something
    }

    $time_end microtime(true);
    $total $time_end $time_start
    This isn't an ideal test. A regular expression needs to be internally parsed and compiled into an execution plan. php obviously does this for you. One thing that's not obvious though is that php will cache this compiled execution plan internally for the duration of the script, making future calls of the exact same regex faster than the first call.

    This severely limits your ability to benchmark the speed of a regex, because very often you only use a given regex a single time per script execution, and so you use the slowest call. So the test in a loop gives the regex an unfair advantage. Well, unless this specific regex will actually be called many times in your script.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $time_start = microtime(true);
    Don't forget that microtime takes some computing power and time too.

    If you are truly fascinated by this kind of thing, then you should install xDebug, and find out about cache.grind files.

  8. #8
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to everybody for explaining! I learned some more things!




    Quote Originally Posted by crmalibu View Post
    I don't know why, but the first time you call microtime() in a script it introduces a lot of variance.

    ...

    This isn't an ideal test. A regular expression needs to be internally parsed and compiled into an execution plan. php obviously does this for you. One thing that's not obvious though is that php will cache this compiled execution plan internally for the duration of the script, making future calls of the exact same regex faster than the first call.

    This severely limits your ability to benchmark the speed of a regex, because very often you only use a given regex a single time per script execution, and so you use the slowest call. So the test in a loop gives the regex an unfair advantage. Well, unless this specific regex will actually be called many times in your script.

    Can I ask where you can find this kind of info? In a particular book? Experiance? A site maybe? This is really interesting!




    Quote Originally Posted by Cups View Post
    If you are truly fascinated by this kind of thing, then you should install xDebug, and find out about cache.grind files.
    I Already had xDebug installed to improve my notepad ++ But I never heard of thos cache.grind files. I hadn't enabled it yet. I have done now and looking at them with wincache. Thanks for the tip, it is what I needed!
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The variance in the first call to microtime() I noticed on my own.

    The regex cache I thought about after seeing in other languages that you could compile a regex and store it in a variable, for when you needed to execute it many times. I wondered why php didn't have such a thing, but then looked and saw it actually did it behind the scenes for us http://www.php.net/manual/en/intro.pcre.php

  10. #10
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    The variance in the first call to microtime() I noticed on my own.

    The regex cache I thought about after seeing in other languages that you could compile a regex and store it in a variable, for when you needed to execute it many times. I wondered why php didn't have such a thing, but then looked and saw it actually did it behind the scenes for us
    So basicly experiance Thank you for sharing that with me
    I still have a long way to go!
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


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
  •