Lacrimas Blog Keep your mind away…

7Dec/110

Time to execute PHP

Hello, today we will talk how to measure executing time of PHP code.
First of all we need to understand what is this. Time to execute is End Seconds - Start seconds.
Lets catch start time:

<?php
function getTime() {
  $a = explode (' ',microtime());
  return(double) $a[0] + $a[1];
}
?>

now we gonna catch start, end time and calculate them.

function CalcTime($stime,$ftime){
  echo "Time to execute: ".number_format(($ftime - $stime),2)." sec.";
}
$startTime = getTime();
//Do something here...
$endTime = getTime();
CalcTime($startTime,$endTime);

On base of this script you can test your script and choose the best way to program something.