Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mabuak/5c5085c5539ec727ac411288a159f478 to your computer and use it in GitHub Desktop.
Save mabuak/5c5085c5539ec727ac411288a159f478 to your computer and use it in GitHub Desktop.
Benchmark
<?php
/**
* @author Ammar Faizi <ammarfaizi2@gmail.com>
* @license MIT
* @link https://t.me/laravelindonesia/111952
*/
for ($i=0; $i<10; $i++) {
echo "Test ".$i."\n";
$start = microtime(true);
$a = "TR".date("ym");
$no = 1;
$string = $no ." = ". $a.str_repeat("0", 3 - strlen($no)).$no.PHP_EOL;
$no = 10;
$string = $no ." = ". $a.str_repeat("0", 3 - strlen($no)).$no.PHP_EOL;
$no = 100;
$string = $no ." = ". $a.str_repeat("0", 3 - strlen($no)).$no.PHP_EOL;
$end = microtime(true);
echo "str_repeat+strlen = ". number_format($t1 = $end - $start, 15)."\n";
$start = microtime(true);
$a = "TR".date("ym");
$no = 1;
$string = $a . sprintf('%03d', $no);
$no = 10;
$string = $a . sprintf('%03d', $no);
$no = 100;
$string = $a . sprintf('%03d', $no);
$end = microtime(true);
echo "sprintf = ". number_format($t2 = $end - $start, 15)."\n";
echo "Which faster? ".($t2 > $t1 ? "str_repeat+strlen" : "sprintf")."\n";
echo "Timediff = ".(number_format(abs($t2-$t1), 15))."\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment