Measure Execution Time of Code in Seconds using PHP

microtime function

<?php

function doSomething(int $seconds): void
{
    sleep($seconds);
}

$startTime = microtime(true);
doSomething(3);
$endTime = microtime(true);

$executionTime = $endTime - $startTime;
echo sprintf('%.8f sec', $executionTime);

Leave a Comment

Cancel reply

Your email address will not be published.