Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Last active August 12, 2017 05:48
Show Gist options
  • Save ngyuki/ef057db6f195e06ce72a03d00a45a0f4 to your computer and use it in GitHub Desktop.
Save ngyuki/ef057db6f195e06ce72a03d00a45a0f4 to your computer and use it in GitHub Desktop.
<?php
namespace strpos {
function startsWith($haystack, $needle) {
return (strpos($haystack, $needle) === 0);
}
function endsWith($haystack, $needle) {
return strrpos($haystack, $needle, 0) === strlen($haystack) - strlen($needle);
}
}
namespace substr {
function startsWith($haystack, $needle) {
return substr($haystack, 0, strlen($needle)) === $needle;
}
function endsWith($haystack, $needle) {
return substr($haystack, -strlen($needle)) === $needle;
}
}
namespace strncmp {
function startsWith($haystack, $needle) {
return strncmp($haystack, $needle, strlen($needle)) === 0;
}
function endsWith($haystack, $needle) {
$haystack = strrev($haystack);
$needle = strrev($needle);
return strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
namespace substr_compare {
function startsWith($haystack, $needle) {
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
}
function endsWith($haystack, $needle) {
return substr_compare($haystack, $needle, - strlen($needle)) === 0;
}
}
namespace preg_match {
function startsWith($haystack, $needle) {
$needle = preg_quote($needle);
return preg_match('/\\A' . $needle . '/', $haystack) !== 0;
}
function endsWith($haystack, $needle) {
$needle = preg_quote($needle);
return preg_match('/' . $needle . '\\z/', $haystack) !== 0;
}
}
namespace {
function test($ns)
{
$startsWith = "$ns\\startsWith";
$endsWith = "$ns\\endsWith";
assert($endsWith('abc', 'c') === true);
assert($endsWith('abc', 'bc') === true);
assert($endsWith('abc', 'abc') === true);
assert($endsWith('abc', 'xabc') === false);
assert($endsWith('abc', 'b') === false);
assert($endsWith('abc', 'ab') === false);
}
function benchmark($ns)
{
gc_collect_cycles();
$startsWith = "$ns\\startsWith";
$endsWith = "$ns\\endsWith";
$str = str_repeat("abc", 1024*1024*20);
$needle = "xxx";
$nums = [];
$t = microtime(true) + 1;
for ($i=0; microtime(true) < $t; $i++) {
$startsWith($needle, $needle);
}
$nums[] = $i;
$t = microtime(true) + 1;
for ($i=0; microtime(true) < $t; $i++) {
$startsWith($str, $needle);
}
$nums[] = $i;
$t = microtime(true) + 1;
for ($i=0; microtime(true) < $t; $i++) {
$endsWith($needle, $needle);
}
$nums[] = $i;
$t = microtime(true) + 1;
for ($i=0; microtime(true) < $t; $i++) {
$endsWith($str, $needle);
}
$nums[] = $i;
printf("%-20s %8d %8d %8d %8d\n", $ns, ...$nums);
}
test('strpos');
test('substr');
test('strncmp');
test('substr_compare');
test('preg_match');
printf("%-20s %8s %8s %8s %8s\n", "", 's:ok', 's:ng', 'e:ok', 'e:ng');
benchmark('strpos');
benchmark('substr');
benchmark('strncmp');
benchmark('substr_compare');
benchmark('preg_match');
}
s:ok s:ng e:ok e:ng
strpos 310957 214 219756 13
substr 241258 251368 251819 255437
strncmp 240180 219722 163450 12
substr_compare 238999 249039 246748 250033
preg_match 210192 220656 224138 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment