Duration time to execute in PHP

albumex picture albumex · May 4, 2012 · Viewed 7.1k times · Source

I need to program something in PHP but i have to know execution time between my line 5 and my line 14.

The problem is that i don't find anything to do what i want (calculate execution time between that lines).

How can i synchronize it with another action ?

Thanks.

Answer

Lawrence Cherone picture Lawrence Cherone · May 4, 2012

Just use microtime(),

Here is a simple example:

<?php
//get initial start time
$time_start = microtime(true);

//then do your php goodness... 

//get script end time
$time_end = microtime(true);

//calculate the difference between start and stop
$time = $time_end - $time_start;

//echo it 
echo "Did whatever in $time seconds\n";
?> 

Or something like this:

<?php
//get initial start time
$time_start = microtime(true);

//then do your php goodness...
sleep(2);

//echo it
echo sprintf("Did whatever in %.3f seconds", (float)microtime(true)-$time_start);
?>