SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there any easy way to test the performance of a php script. I am trying to write mine as economically as possible but would love to be able to tell if doing something this way is faster than doing it this other way. What is the best way to do this? would a timer at the top and bottom of the script be best do you think?

    thanks.

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after more searching i found the answer in the developer's cookbook book.

    PHP Code:
    // array for time testing
    function debug_timing($label) {
    static 
    $basetime,
            
    $totaltime,
            
    $rpttimes;

    if (
    $label == 'init') {
        
    $rpttimes = array();
        
    $basetime microtime();
        
    $totaltime 0;
        
    ereg ("^([^ ]+) (.+)"$basetime$r);
        
    $basetime doubleval ($r[2]) + doubleval ($r[1]);
        return;
        }

    if (
    $label == 'print') {
    // send to screen
    echo "<B>Timing Results</B><BR>\n";
    for (
    $i=0$i count ($rpttimes); $i++) {
    echo 
    $rpttimes[$i]\n<br>";
    }
    echo 
    "total: $totaltime\n";
    return;
    }

    // record current elapsed time, accumulate
    $newtime microtime();
    ereg ("^([^ ]+) (.+)"$newtime$r);
    $newtime doubleval ($r[2]) + doubleval ($r[1]);
    $diff $newtime $basetime;
    $rpttimes[] = sprintf ("%-20s %s"$label$diff);
    $basetime $newtime;
    $totaltime += $diff;


    that is a class and then call like

    debug_timing('place_1');

    do something here

    debug_timing('place_2');

    then at the end do

    debug_timing('print');

    and you will get a list of times to each point.

    Only problem is the total times and the first time are not working properly, not sure why but just do 2 times when you want the first one and it will work fine. If anyone knows what the mistake is, either in the book or my copying of it let me know.


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
  •