Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duration time to execute in PHP

Tags:

php

execute

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.

like image 238
albumex Avatar asked Nov 29 '22 17:11

albumex


1 Answers

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);
?> 
like image 74
Lawrence Cherone Avatar answered Dec 10 '22 11:12

Lawrence Cherone