Skip to content

Instantly share code, notes, and snippets.

@lempzz
Last active May 26, 2022 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lempzz/3b20b7c8ce867259232ca9ac564bbca8 to your computer and use it in GitHub Desktop.
Save lempzz/3b20b7c8ce867259232ca9ac564bbca8 to your computer and use it in GitHub Desktop.
Small PHP stopwatch for track how long your code executing
<?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