// Measuring execution time using microtime(true)
// A sample function to measure
function work() {
$sum = 0;
for ($i = 0; $i < 100000000; $i++) {
$sum += $i;
}
}
// Record start time (in seconds with microsecond precision)
$start = microtime(true);
work();
// Record end time
$end = microtime(true);
// Compute elapsed time
$elapsedSeconds = $end - $start;
$elapsedMilliseconds = $elapsedSeconds * 1000;
echo "Execution time: {$elapsedMilliseconds} ms\n";
echo "Execution time: {$elapsedSeconds} seconds\n";
/*
run:
Execution time: 390.32816886902 ms
Execution time: 0.39032816886902 seconds
*/