Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Timing Script Execution
Getting Clever
The problem with the previous script is that what happens when you want to find exactly which part of your script is taking all the time? You have to move the start time and end time bits around all over the script and write down the results, comparing notes and invariably losing half the little bits of paper you wrote them down on. What if we want to create markers all the way through our script and see the time it takes between each marker? Sounds like a lot code to add into our scripts, so what I did instead was create a timer object, which we could use to time our scripts.
The timer object
Our object will only need one property - an array of the times that we have marked parts of the script. I also added in a descriptor for the time. Our object will have to have a number of methods - a method to start the timer, a method to stop the timer and a method to add markers for any points in between. We will also need a method to display the information that we have collected. Because I found that a lot the code was being duplicated, I created a function to join the times together.
Here then is the code for the include file phptimer.inc. I've added comments through the code as explanation, but if you are happy with arrays and understand a bit of OO programming, you should have no worries:

<?php

// begin class PHP_timer
class PHP_timer {

    
// array to store the information that we collect during the script
    // this array will be manipulated by the functions within our object
    
var $points = array();
    
    
// call this function at the beginning of the script
    
function start() {
        
// see the addmarker() function later on
        
$this->addmarker("Start");
    }
    
// end function start()

    // call this function at the end of the script
    
function stop() {
        
// see the addmarker() function later on
        
$this->addmarker('Stop');
    }
    
// end function stop()

    // this function is called to add a marker during the scripts execution
    // it requires a descriptive name
    
function addmarker($name) {
        
// call the jointime() function and pass it the output of the microtime() function
        //  as an argument
        
$markertime = $this->jointime(microtime());
        
// $ae (stands for Array Elements) will contain the number of elements
        // currently in the $points array
        
$ae = count($this->points);
        
// store the timestamp and the descriptive name in the array
        
$this->points[$ae][0] = $markertime;
        
$this->points[$ae][1] = $name;
    }
    
// end function addmarker()

    // this function manipulates the string that we get back from the microtime() function
    
function jointime($mtime) {
        
// split up the output string from microtime() that has been passed
        // to the function
        
$timeparts = explode(' ',$mtime);
        
// concatenate the two bits together, dropping the leading 0 from the
        // fractional part
        
$finaltime = $timeparts[1].substr($timeparts[0],1);
        
// return the concatenated string
        
return $finaltime;
    }
    
// end function jointime()
    
    // this function simply give the difference in seconds betwen the start of the script and
    // the end of the script
    
function showtime() {
        echo
bcsub($this->points[count($this->points)-1][0],$this->points[0][0],6);
    }
    
// end function showtime()
    
    // this function displays all of the information that was collected during the
    // course of the script
    
function debug() {
        echo
'Script execution debug information:';
        echo
"<table border="0" cellspacing="5" cellpadding="5">\n";
        
// the format of our table will be 3 columns:
        // Marker name, Timestamp, difference
        
echo "<tr><td><b>Marker</b></td><td><b>Time</b></td><td><b>Diff</b></td></tr>\n";
        
// the first row will have no difference since it is the first timestamp
        
echo "<tr>\n";
        echo
'<td>'.$this->points[0][1].'</td>';
        echo
'<td>'.$this->points[0][0].'</td>';
        echo
"<td>-</td>\n";
        echo
"</tr>\n";
        
// our loop through the $points array must start at 1 rather than 0 because we have
        // already written out the first row
        
for ($i = 1; $i < count($this->points);$i++) {
            echo
"<tr>\n";
            echo
'<td>'.$this->points[$i][1].'</td>';
            echo
'<td>'.$this->points[$i][0].'</td>';
            echo
'<td>';
            
// write out the difference between this row and the previous row
            
echo bcsub($this->points[$i][0],$this->points[$i-1][0],6);
            echo
'</td>';
            echo
"</tr>\n";
        }
        echo
'</table>';
    }
    
// end function debug()
}
// end class PHP_timer

?>

[ Next Page ]

[Page 1]  [Page 2]  


Comments:
RE: PHP is slower than HTMLMalph03/27/04 01:42
no bcsub?zariok01/08/03 16:54
about phpdimas yurisdiansyah08/07/02 02:41
RE: scheduling a php script on a web servergordon07/11/02 03:52
this might be easierJason E. Sweat06/17/02 17:17
CPU execution timeMarcel Schiffer06/10/02 05:08
TimerMojgan05/27/02 06:19
RE: scheduling - the easy wayNkm05/15/02 22:54
RE: scheduling - the easy wayKerry Slavin04/11/02 00:52
RE: scheduling - the easy wayGlen Hassell04/10/02 07:51
RE: scheduling a php script on a web serverNkm04/05/02 20:08
scheduling a php script on a web serverAlain04/05/02 11:14
RE: PHP is slower than HTMLWill V.03/26/02 22:21
RE: PHP is slower than HTMLBerge Schwebs Bjørlo01/26/02 08:48
RE: help:Fatal error: Call to undefined function:em1x01/18/02 03:31
PHP is slower than HTMLPeter11/28/01 00:08
total time vs. time used to process scriptbart ogryczak11/16/01 11:08
RE: Why don't you simply add the two numbers??Benoit10/04/01 10:25
help:Fatal error: Call to undefined function:sprming05/09/01 18:10
RE: date functionjellings11/02/00 15:12
Why don't you simply add the two numbers??Mac Wang10/25/00 01:23
date functionPrem Kumar10/24/00 03:23
more timing functions here :philip olson10/06/00 22:48
Providing article code as seperate text filegh (they wanted it longer, so here goes)10/03/00 23:59
RE: time difference from server to servergh (they wanted it longer, so here goes)10/03/00 23:52
time difference from server to serverjaydj10/03/00 05:44
RE: bcsub vs roundVladimir Shapiro10/03/00 04:28
RE: databaseVladimir Shapiro10/03/00 04:21
just in time!Oliwier Ptak10/02/00 18:22
bcsub vs roundDouglas E. Warner09/30/00 11:21
Warning: short excecution timesUlf Wendel09/27/00 10:48
An example from Cold FusionJeremy Bailey09/27/00 08:31
databaseUioreanu Calin09/27/00 07:42
databaseUioreanu Calin09/27/00 06:44
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.