PHP runtime

I need to program something in PHP, but I need to know the runtime between my line 5 and my line 14.

The problem is that I cannot find anything to do (calculate the runtime between these lines).

How can I synchronize it with another action?

Thank.

+3
source share
2 answers

I made a post about this on my website after some searching.

You can add:

$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 understand by synchronizing it? Run something else? More precisely for this part.

+7
source

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);
?> 
+10
source

All Articles