logo

"We need a web site."

Now what?
When you have plenty of ideas, but not a lot of answers, the next step is WeavingtheWeb.com.

Enter your email address here to subscribe to the "PHP Tip of the Week," emailed to you every Monday morning. Enjoy!

 

PHP Tip of the Week Archive

  • If Brevity: The Ternary Operator
  • Easy Database Inserts
  • Output Buffering and Editing Web Pages On the Fly
  • Searching Arrays
  • Random Thoughts
  • Faster Page Transfer Using Compression
  • Persistent Values in Web Forms
  • Using HEREDOC Syntax with HTML
  • Calculating Dates More Easily
  • Using Single and Double Quotes
  • Timing Your PHP Scripts

    You can fine-tune the performance of your PHP scripts by timing the execution of those scripts. This can be very helpful in determining if one portion of a script is particularly slow, thus slowing the overall execution time.

    This first simple timer function is available on most systems, and so most accessible to all:

    <?php

    //First we need two timing functions:

    //Format the output of calls to microtime()
    function get_microtime() {
      $mtime = microtime();
      $mtime = explode(" ",$mtime);
      $mtime = doubleval($mtime[1]) + doubleval($mtime[0]);
      return $mtime;
    }

    //Format the difference between two benchmark times
    function show_time($time1,$time2) {
      $time = abs($time1 - $time2);
      return sprintf("%.4f",$time) . " seconds";
    }

    //Set the first timer
    $time1 = get_microtime();

    //Execute some code
    for ($i=0; $i<100; $i++) {
      print "Value of i is $i<br>\n\n";
    }

    //Set the second timer
    $time2 = get_microtime();

    //Get total execution time:
    print "Total execution time: " . show_time($time1,$time2);

    ?>

    get_microtime() can be used to set timers at various places within the script where you suspect major problems with performance. Then feed those timers to show_time() to calculate the difference in seconds between them.

    getrusage() is also helpful to time script execution, although getrusage() is not available in a Windows environment.

    <?php

    $first = getrusage();

    $ubefore = $first["ru_utime.tv_sec"] . "." . str_pad($first["ru_utime.tv_usec"],6,'0',STR_PAD_LEFT);
    $sbefore = $first["ru_stime.tv_sec"] . "." . str_pad($first["ru_stime.tv_usec"],6,'0',STR_PAD_LEFT);


    //Execute some code
    for ($i=0; $i<10000; $i++) {
      print "Value of i is $i<br>\n\n";
    }

    $second = getrusage();

    $uafter = $second["ru_utime.tv_sec"]. "." . str_pad($second["ru_utime.tv_usec"],6,'0',STR_PAD_LEFT);
    $safter = $second["ru_stime.tv_sec"]. "." . str_pad($second["ru_stime.tv_usec"],6,'0',STR_PAD_LEFT);

    $utime_elapsed = ($uafter - $ubefore);
    $stime_elapsed = ($safter - $sbefore);

    echo "Elapsed user time: $utime_elapsed seconds<br>";
    echo "Elapsed system time: $stime_elapsed seconds";

    ?>

    Finally, when the PEAR libraries are available to PHP, use the Benchmark class to time various portions of a script. More about that here:

    http://pear.php.net/package/Benchmark/docs/latest/Benchmark/Benchmark_Timer.html

    One more note: When checking the performance of a script with timers, execute the script multiple times and take the average of the timing results. The more results you use in that average, the more accurate your performance assessment will be.

    Take time to test performance, and you'll save time in the long run.

  • When Nothing is Something: Zero, Empty, Null, and Negative Values
  • Nesting Instincts
  • Useful (for) Characters: The Increment Operator
  • The "static" keyword
  • Those !#@% Characters!
  • The Value of PHP Classes and OOP
  • The Simple Things in (programming) Life
  • Just a Passing Reference
  • Simple Image Creation with PHP