SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: timers

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    timers

    I want to perform some tests for how long different PHP things take.

    The way I found is this:

    PHP Code:
    $stime explode(' 'microtime()); 
    #test stuff here
    $etime explode(' 'microtime());  

    $etime $etime[1] + $etime[0]; 
    $stime $stime[1] + $stime[0]; 

    $totaltime number_format($etime $stime,10); 
    (actually the one I found is worse than this, and I optimized it slightly)

    The timers themselves take about 0.00003500 sec (average over 100 tries) for them to do their stuff. How can I make it so it ONLY times the stuff on the inside? As in, if I do

    <timestart>
    <timeend>

    Is there a way for it to say 0.0000000000 seconds here?

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't think it matters. There was a thread a couple of months ago where microtime was discussed. It's inconsistent, varies across machines and generally provides non-exact values, so I wouldn't worry about it. Also, it seems to be slightly more precise when you do a dummy first:
    PHP Code:
    microtime();
    $start microtime();
    //lots of PHP
    $end microtime();
    echo 
    $end $start

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think its necessarily more accurate. I just think the first microtime takes a certain amount of time to load the library for which microtime is part of.

    Earlier I tested that, and for my server it took .000027 seconds more for the first microtime.

    And yes, I know... all times are dependent upon the server. But tests would give a relative speed.

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, maybe if you find that thread it will help you. It was started by me.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I found the thread... it pretty much said exactly what was said here.

    Does do you of any websites where someone has done some speeds tests comparing various similar blocks of code to find out which is more efficient?

    I didn't want to do many tests (they are a little time consuming), but I did three:

    test 1: nothing
    measures the amount of time the timers take on average
    100 tries. average time: .00003500 seconds
    (this time will be subtracted from all other tests)

    test 2: $string = "hello world this is a string of some sort.";
    100 tries. average time: .00000103 seconds

    test 3: $string = 'hello world this is a string of some sort.';
    100 tries. average time: .00000082 seconds

    That test shows that single quotes are significantly faster for storing strings than double quotes are.



    Anyway, there are many different tests I'd like to run (and with larger amounts), but if someone else has done this already I don't want to waste my time.

    Another thing I'd like to test is:
    echo 'test1test2test3test4test5test6test7test8';
    vs
    echo 'test1','test2','test3','test4','test5','test6','test7','test8';
    vs
    echo 'test1'.'test2'.'test3'.'test4'.'test5'.'test6'.'test7'.'test8';

    The sticky thread "PHP and MySQL coding tips" says certain things are faster, but it'd be nice to see some test results.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Regarding test 3, using single quotation marks is faster, but not significantly. You're talking about differences in millionths of a second.

    I found this: http://pear.php.net/package/Benchmark - might be useful for you.


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
  •