Page 1 of 1

measuring performance in PHP - how?

Posted: Thu Mar 25, 2004 6:49 am
by davidklonski
Hello

I would like to test how fast my PHP scripts are executed.
I was thinking of calculating the current time (if millis - if possible) at the beginning of the script (server side) and to send that value into a javascript variable.
When the page loads (onload() event), I calculate again the the current time in javascript and just subtract the latter from the first.

I am hoping that this will give me the time in millis from the moment the page reached the server until the time it was loaded in the client.

Does anyone know of a way to obtain the current time in millis?
Does anyone know of a better way to calculate performance?

thanks

Posted: Thu Mar 25, 2004 6:51 am
by JayBird
i do it like this

Code: Select all

//put at begining of file

function utime (){
$time = explode( " ", microtime());
$usec = (double)$time[0];
$sec = (double)$time[1];
return $sec + $usec;
}
$start = utime();

Code: Select all

//put at end of page before /body and /h // tml tags
$end = utime();
$run = $end - $start;
echo "Page created in " . substr($run, 0, 5) . " secs.";
Mark

Posted: Thu Mar 25, 2004 7:39 am
by davidklonski
If the microtime() functions returns the time in micro seconds in the form of "msec sec"

where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds.

isn't there a way to get the number of Milliseconds since 0:00:00 January 1, 1970 GMT?

thanks

Posted: Thu Mar 25, 2004 9:39 am
by m3mn0n
The snipplet of code Bech posted works great, I highly recommend using it.

Posted: Thu Mar 25, 2004 9:49 am
by pickle
To get milliseconds from the epoch, just do

Code: Select all

time() * 1000