Created
December 1, 2022 09:36
-
-
Save bluefirex/42587c48ae5025e9c30916f1fc4d76a0 to your computer and use it in GitHub Desktop.
Compare runtime of PHP functions
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 | |
function benchmark(Callable $fn, ...$args) { | |
$start = microtime(true); | |
for ($i = 0; $i <= 23005; $i++) { | |
$fn(...$args); | |
} | |
$end = microtime(true); | |
$diff = ($end - $start); | |
return round($diff, 6); | |
} | |
var_dump( | |
benchmark(function() { | |
$str = str_repeat('0', 36); | |
}), | |
benchmark(function() { | |
$str = str_pad('', 36, '0', STR_PAD_LEFT); | |
}), | |
benchmark(function() { | |
$str = str_pad('', 36, '0', STR_PAD_RIGHT); | |
}) | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment