Skip to content

Instantly share code, notes, and snippets.

@bluefirex
Created December 1, 2022 09:36
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 bluefirex/42587c48ae5025e9c30916f1fc4d76a0 to your computer and use it in GitHub Desktop.
Save bluefirex/42587c48ae5025e9c30916f1fc4d76a0 to your computer and use it in GitHub Desktop.
Compare runtime of PHP functions
<?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