I’m trying to use php’s microtime() to benchmark how long two loops in my code take, to see which one runs faster. Here’s my code:
$now = microtime();
//loop1 here
$elapsed = microtime() - $now;
echo "Elapsed: $elapsed <br />";
When I run this for one loop, I get the result in negative numbers, something like:
Elapsed: -0.120572
This is strange, since microtime() should always be bigger than its value a few microseconds earlier?
For the second loop, the value is always positive:
Elapsed: 0.005483
What’s going on here?
use
microtime(true)as per https://www.php.net/manual/en/function.microtime.php as false returns the microtime as a string and your performing maths on strings and not as numbers..