Created
June 5, 2018 23:08
-
-
Save mabuak/5c5085c5539ec727ac411288a159f478 to your computer and use it in GitHub Desktop.
Benchmark
This file contains 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 | |
/** | |
* @author Ammar Faizi <[email protected]> | |
* @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