PHPBuilder - Timing Script Execution



RSS Twitter
Articles Tricks And Hacks

Timing Script Execution

by: Allan Kent
|
September 26, 2000

Timing Script Execution

No doubt you've all seen on Google's search page where it tells you how long your search took to complete. This, and some talk I noticed a while back in one of the forums about some of the scripts that were taking a while to execute made me think about how you would go about timing your scripts.
The easiest way of timing a script would be to get the time at the beginning of the script and then get the time at the end of the script. The difference between the two is the time it took your script to run. PHP has a function time() which returns the number of seconds since 1970. Now I can't speak for everyone here, but typically the scripts that I write tend to finish running within a second. So measuring the time in seconds is not really going to help me. A topical example would be using something like an egg timer to time the 100m Olympic final - unless someone breaks both their legs on the starting blocks, all the runners will be within 1 egg timer. The microtime() function gives us what we need - the fractional part of the second at the time that the function was called. It also gives us the number of seconds since 1970 like time() does, so we don't need to call both functions. The only problem with the microtime() function is that it returns to us a string with 2 numbers in it - the seconds part, and the fractional part - separated by a space.
<?php

  
echo microtime();

?>
Returns:
0.13554500 968578758
This is not all that useful to us when trying to work out differences, so what we need to do is drop the 0 from the front of the fractional part and tack the rest onto the end of the seconds part:

<?php

  $timeparts 
explode(' ',microtime());
  
$thetime $timeparts[1].substr($timeparts[0],1);

?>
Both the explode() and substr() functions are well documented in the manual and have been dealt with on numerous occasions on this site, so I'm not going to waste time going through them here again.
Once we have 2 times (a start and finish) we can subtract the start time from the finish time and we'll have the difference. If you use the bcsub() function, remember to indicate to the function how many places after the decimal point you want to preserve. If you don't, then it'll default to integers and you'll be back in the egg timer scenario again.

<?php

  $timeparts 
explode(' ',microtime());
  
$starttime $timeparts[1].substr($timeparts[0],1);
  
$timeparts explode(' ',microtime());
  
$endtime $timeparts[1].substr($timeparts[0],1);
  echo 
bcsub($endtime,$starttime,6)

?>
returns to us
0.000457
You can now pop that code in amongst your script to debug what's taking so long in your script.


1
|
2
|
3
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Allan Kent

Comment:



Comment:

(Maximum characters: 1200). You have characters left.