4

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.

albumex
  • 53
  • 1
  • 3

2 Answers2

15

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);
?> 
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
8

I made a post about it on my website after some search.

You can add before :

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

And after your code :

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "Executed in ".$totaltime." seconds";

What do you mean by synchonizing it ? Launching something else ? Be more precise for that part.

ChapMic
  • 26,954
  • 1
  • 21
  • 20