Simple Benchmarking in PHP ⏳
Sometimes I want a quick and simple way to know how long a block of code took to run. The usual way you see it done is something like this:
$startTime = microtime(true); // Code to test $endTime = microtime(true); $time = $endTime - $startTime;
$time
contains the number of seconds which it took for the code to run.
There is a simpler way:
$time = -microtime(true); // Code to test $time += microtime(true);
This automatically calculates the difference between start and end without needing the extra step.
The same cane be done for memory usage:
$memory = -memory_get_usage(); // Code to test $memory += memory_get_usage();