Created
December 27, 2019 17:23
-
-
Save Miguel-Serejo/4bc70175ad0bb60b3261d1100f249cf5 to your computer and use it in GitHub Desktop.
Benchmarking different startsWith strategies
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 | |
$providers = [ | |
'Illuminate\Auth\AuthServiceProvider', | |
'Illuminate\Broadcasting\BroadcastServiceProvider', | |
'Illuminate\Bus\BusServiceProvider', | |
'Illuminate\Cache\CacheServiceProvider', | |
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', | |
'Illuminate\Cookie\CookieServiceProvider', | |
'Illuminate\Database\DatabaseServiceProvider', | |
'Illuminate\Encryption\EncryptionServiceProvider', | |
'Illuminate\Filesystem\FilesystemServiceProvider', | |
'Illuminate\Foundation\Providers\FoundationServiceProvider', | |
'Illuminate\Hashing\HashServiceProvider', | |
'Illuminate\Mail\MailServiceProvider', | |
'Illuminate\Notifications\NotificationServiceProvider', | |
'Illuminate\Pagination\PaginationServiceProvider', | |
'Illuminate\Pipeline\PipelineServiceProvider', | |
'Illuminate\Queue\QueueServiceProvider', | |
'Illuminate\Redis\RedisServiceProvider', | |
'Illuminate\Auth\Passwords\PasswordResetServiceProvider', | |
'Illuminate\Session\SessionServiceProvider', | |
'Illuminate\Translation\TranslationServiceProvider', | |
'Illuminate\Validation\ValidationServiceProvider', | |
'Illuminate\View\ViewServiceProvider', | |
'App\Providers\AppServiceProvider', | |
'App\Providers\AuthServiceProvider', | |
'App\Providers\RouteServiceProvider', | |
]; | |
$nb = 1000000; | |
foreach($providers as $provider) { | |
$t1 = microtime(true); | |
for ($i = $nb; $i--; ) { | |
substr($provider, 0, strlen('Illuminate\\')) === 'Illuminate\\'; | |
} | |
$t2 = microtime(true); | |
for ($i = $nb; $i--; ) { | |
strpos($provider, 'Illuminate\\') === 0; | |
} | |
$t3 = microtime(true); | |
for ($i = $nb; $i--; ) { | |
Str::startsWith($provider, 'Illuminate\\'); | |
} | |
$t4 = microtime(true); | |
$times = [ | |
'substr' => $t2 - $t1, | |
'strpos' => $t3 - $t2, | |
'startswith' => $t4 - $t3, | |
]; | |
asort($times); | |
echo "Provider: $provider" . PHP_EOL; | |
print_r($times); |
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
Provider: Illuminate\Auth\AuthServiceProvider | |
Array | |
( | |
[strpos] => 0.072001934051514 | |
[substr] => 0.12313508987427 | |
[startswith] => 0.27450609207153 | |
) | |
Provider: Illuminate\Broadcasting\BroadcastServiceProvider | |
Array | |
( | |
[strpos] => 0.070228099822998 | |
[substr] => 0.12977004051208 | |
[startswith] => 0.27725291252136 | |
) | |
Provider: Illuminate\Bus\BusServiceProvider | |
Array | |
( | |
[strpos] => 0.072039127349854 | |
[substr] => 0.13160586357117 | |
[startswith] => 0.28314590454102 | |
) | |
Provider: Illuminate\Cache\CacheServiceProvider | |
Array | |
( | |
[strpos] => 0.07361888885498 | |
[substr] => 0.12834000587463 | |
[startswith] => 0.27815103530884 | |
) | |
Provider: Illuminate\Foundation\Providers\ConsoleSupportServiceProvider | |
Array | |
( | |
[strpos] => 0.074341058731079 | |
[substr] => 0.13100600242615 | |
[startswith] => 0.27711987495422 | |
) | |
Provider: Illuminate\Cookie\CookieServiceProvider | |
Array | |
( | |
[strpos] => 0.075760841369629 | |
[substr] => 0.12340903282166 | |
[startswith] => 0.28450298309326 | |
) | |
Provider: Illuminate\Database\DatabaseServiceProvider | |
Array | |
( | |
[strpos] => 0.07166314125061 | |
[substr] => 0.12988090515137 | |
[startswith] => 0.27933788299561 | |
) | |
Provider: Illuminate\Encryption\EncryptionServiceProvider | |
Array | |
( | |
[strpos] => 0.082395076751709 | |
[substr] => 0.12779498100281 | |
[startswith] => 0.28107500076294 | |
) | |
Provider: Illuminate\Filesystem\FilesystemServiceProvider | |
Array | |
( | |
[strpos] => 0.072663068771362 | |
[substr] => 0.13360500335693 | |
[startswith] => 0.28483891487122 | |
) | |
Provider: Illuminate\Foundation\Providers\FoundationServiceProvider | |
Array | |
( | |
[strpos] => 0.075443029403687 | |
[substr] => 0.12512111663818 | |
[startswith] => 0.27893304824829 | |
) | |
Provider: Illuminate\Hashing\HashServiceProvider | |
Array | |
( | |
[strpos] => 0.071493148803711 | |
[substr] => 0.12965297698975 | |
[startswith] => 0.27605295181274 | |
) | |
Provider: Illuminate\Mail\MailServiceProvider | |
Array | |
( | |
[strpos] => 0.071806907653809 | |
[substr] => 0.13145995140076 | |
[startswith] => 0.27304601669312 | |
) | |
Provider: Illuminate\Notifications\NotificationServiceProvider | |
Array | |
( | |
[strpos] => 0.074597835540771 | |
[substr] => 0.12284398078918 | |
[startswith] => 0.27880120277405 | |
) | |
Provider: Illuminate\Pagination\PaginationServiceProvider | |
Array | |
( | |
[strpos] => 0.068490028381348 | |
[substr] => 0.12517094612122 | |
[startswith] => 0.27783393859863 | |
) | |
Provider: Illuminate\Pipeline\PipelineServiceProvider | |
Array | |
( | |
[strpos] => 0.073277950286865 | |
[substr] => 0.13213014602661 | |
[startswith] => 0.27314805984497 | |
) | |
Provider: Illuminate\Queue\QueueServiceProvider | |
Array | |
( | |
[strpos] => 0.07514500617981 | |
[substr] => 0.1423180103302 | |
[startswith] => 0.28454995155334 | |
) | |
Provider: Illuminate\Redis\RedisServiceProvider | |
Array | |
( | |
[strpos] => 0.066940784454346 | |
[substr] => 0.18014216423035 | |
[startswith] => 0.25082206726074 | |
) | |
Provider: Illuminate\Auth\Passwords\PasswordResetServiceProvider | |
Array | |
( | |
[strpos] => 0.072724103927612 | |
[substr] => 0.11324310302734 | |
[startswith] => 0.27059698104858 | |
) | |
Provider: Illuminate\Session\SessionServiceProvider | |
Array | |
( | |
[strpos] => 0.064407825469971 | |
[substr] => 0.16698908805847 | |
[startswith] => 0.27252101898193 | |
) | |
Provider: Illuminate\Translation\TranslationServiceProvider | |
Array | |
( | |
[strpos] => 0.05722188949585 | |
[substr] => 0.10263609886169 | |
[startswith] => 0.23073506355286 | |
) | |
Provider: Illuminate\Validation\ValidationServiceProvider | |
Array | |
( | |
[strpos] => 0.056895971298218 | |
[substr] => 0.10267806053162 | |
[startswith] => 0.22989797592163 | |
) | |
Provider: Illuminate\View\ViewServiceProvider | |
Array | |
( | |
[strpos] => 0.06043004989624 | |
[substr] => 0.10628986358643 | |
[startswith] => 0.23639607429504 | |
) | |
Provider: App\Providers\AppServiceProvider | |
Array | |
( | |
[strpos] => 0.057359933853149 | |
[substr] => 0.10387992858887 | |
[startswith] => 0.24357295036316 | |
) | |
Provider: App\Providers\AuthServiceProvider | |
Array | |
( | |
[strpos] => 0.056458234786987 | |
[substr] => 0.10904884338379 | |
[startswith] => 0.24451684951782 | |
) | |
Provider: App\Providers\RouteServiceProvider | |
Array | |
( | |
[strpos] => 0.057311058044434 | |
[substr] => 0.10585999488831 | |
[startswith] => 0.24377584457397 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment