Why is this microtime showing up weird in PHP
$start4 = microtime(true);
// run some php code
$end4 = microtime(true);
print \"Time4: \". ($end4 - $start
It seems that this microtime()
is showing up weird because PHP has a threshold, on either side of which it displays either a number in scientific notation or one in decimal notation. Both of these are technically "floats" (see documentation).
It seems that this threshold is somewhere between 0.8 seconds and 0.9 seconds; at least that's what my tests concluded. Using the following code will show scientific notation:
$start4 = microtime(true);
sleep(0.8);
$end4 = microtime(true);
echo 'Time4: ' . ($end4 - $start4) . '
';
But if we change our wait time to sleep(0.9)
, a decimal number is produced. This may or may not be the case on all systems or installations, but this is at least what my tests showed.
You can counteract this yourself by using the sprintf() function, like this:
$start4 = microtime(true);
sleep(0.8);
$end4 = microtime(true);
echo 'Time4: ' . sprintf('%f', $end4 - $start4) . '
';
This will always show the time as a decimal number.