Last active
October 14, 2020 03:10
-
-
Save mnpenner/9a941ab830a457ed0358 to your computer and use it in GitHub Desktop.
Various StartsWith implementations
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 | |
function substr_startswith($haystack, $needle) { | |
return substr($haystack, 0, strlen($needle)) === $needle; | |
} | |
function preg_match_startswith($haystack, $needle) { | |
return preg_match('~' . preg_quote($needle, '~') . '~A', $haystack) > 0; | |
} | |
function substr_compare_startswith($haystack, $needle) { | |
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0; | |
} | |
function strpos_startswith($haystack, $needle) { | |
return strpos($haystack, $needle) === 0; | |
} | |
function strncmp_startswith($haystack, $needle) { | |
return strncmp($haystack, $needle, strlen($needle)) === 0; | |
} | |
function strncmp_startswith2($haystack, $needle) { | |
return $haystack[0] === $needle[0] | |
? strncmp($haystack, $needle, strlen($needle)) === 0 | |
: false; | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | |
echo 'generating tests'; | |
for($i = 0; $i < 100000; ++$i) { | |
if($i % 2500 === 0) echo '.'; | |
$test_cases[] = [ | |
random_bytes(random_int(1, 7000)), | |
random_bytes(random_int(1, 3000)), | |
]; | |
} | |
echo "done!\n"; | |
$functions = ['substr_startswith', 'preg_match_startswith', 'substr_compare_startswith', 'strpos_startswith', 'strncmp_startswith', 'strncmp_startswith2']; | |
$results = []; | |
foreach($functions as $func) { | |
$start = microtime(true); | |
foreach($test_cases as $tc) { | |
$func(...$tc); | |
} | |
$results[$func] = (microtime(true) - $start) * 1000; | |
} | |
asort($results); | |
foreach($results as $func => $time) { | |
echo "$func: " . number_format($time, 1) . " ms\n"; | |
} |
That looks the same as strncmp_startswith2, what's the difference?
…On Tue, Oct 13, 2020, 8:52 AM PerfilovStanislav ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
function strncmp_startswith3(string $haystack, string $needle) {
return $haystack[0] === $needle[0]
? \strncmp($haystack, $needle, \strlen($needle)) === 0
: false;
}
is faster
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/9a941ab830a457ed0358#gistcomment-3487828>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAIPXJV2H5UAIPCACTTEXTTSKRZU7ANCNFSM4SPEVZLQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is faster