measuring performance in PHP - how?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

measuring performance in PHP - how?

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

The snipplet of code Bech posted works great, I highly recommend using it.
Last edited by m3mn0n on Thu Mar 25, 2004 10:00 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

To get milliseconds from the epoch, just do

Code: Select all

time() * 1000
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply