The PHP function microtime() returns a similar value to time(), but returns a more accurate floating-point representation of the number of seconds since the Unix Epoch. It is not usually used in the same way as the previous time functions, but is used more often when debugging PHP code and analysing the time of execution of functions for performance enhancement purposes
microtime() only takes one argument, and that is a boolean value stating whether or not to return the value as a floating point number rather than a formatted string. Using microtime() without this parameter can produce some confusing results if you’re not aware of its behaviour. Notice here how the first call returns the time as a string split into microseconds and seconds
echo microtime(); // produces 0.61273200 1219075923 echo microtime(true); // produces 1219075923.6128
If you try using the value of the first function call directly, PHP will treat it as a string, and give inaccurate results.
Here is an example of using microtime() to measure the performance of a for-loop compared with a while-loop performing 10 million repetitions of a single increment operation. Before each loop, we record the start time using microtime(), and compare it to the end time after each loop is complete.
$while_start = microtime(true);
$count = 0;
while($count < 10000000){
$count++;
}
$duration_while = (microtime(true)-$while_start)*1000;
printf ("While loop execution in %.3f ms", $duration_while);
$for_start = microtime(true);
$count = 0;
for($i=0; $i<10000000; $i++){
$count++;
}
$duration_for = (microtime(true)-$for_start)*1000;
printf ("For loop execution in %.3f ms", $duration_for);
This code produces the following result (based upon execution times on my system)
While loop execution in 787.590 ms For loop execution in 1363.392 ms
We also multiply the values on lines 6 and 13 to get a value in microseconds, rather than seconds




