Last active
May 26, 2022 07:10
-
-
Save lempzz/3b20b7c8ce867259232ca9ac564bbca8 to your computer and use it in GitHub Desktop.
Small PHP stopwatch for track how long your code executing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Stopwatch | |
{ | |
public static function launch(bool $asMicrotime = true, int $precision = 4) : callable | |
{ | |
$start = $asMicrotime ? microtime(true) : time(); | |
return function () use ($start, $asMicrotime, $precision) : string { | |
$finish = $asMicrotime ? microtime(true) : time(); | |
return number_format($finish - $start, $precision, '.', ''); | |
}; | |
} | |
} | |
// Example of usage: | |
$executionTime = Stopwatch::launch(); | |
// your code here ... | |
sleep(1); | |
echo 'Duration ' . $executionTime(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment