<?php
$str = str_repeat("a\u{0308}bc", 1000);
echo '$str = ', $str, "\n";
function test($name, $test) {
$t = microtime(true);
for ($i = 0; $i < 1000; ++$i) {
$test();
}
echo sprintf('%.6f', (microtime(true) - $t)), 's ', $name, "\n";
}
foreach (["a\u{0308}bc", 'abc', str_repeat("a\u{0308}bc", 20).'a'] as $prefix) {
echo "\n", '$prefix = ', $prefix, "\n";
test('grapheme_strpos', function () use ($str, $prefix) {
return 0 === grapheme_strpos($str, $prefix);
});
test('grapheme_substr', function () use ($str, $prefix) {
return $prefix === grapheme_substr($str, 0, grapheme_strlen($prefix));
});
test('grapheme_extract', function () use ($str, $prefix) {
return $prefix === grapheme_extract($str, strlen($prefix), GRAPHEME_EXTR_MAXBYTES);
});
}
preferences:
23.1 ms | 406 KiB | 5 Q