<?php
function letterSequencePregSplit($string) {
$stringAsArray = preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY
); $arrayCount = count($stringAsArray);
for ($i = 0; $i < $arrayCount; $i++) {
yield $stringAsArray[$i];
}
}
function letterSequenceMbSubstr($string) {
for ($i = 0; $i < $stringLength; $i++) {
}
}
function getAverageDurationFor(callable $generator, $string, $attempts = 1000) {
$timeOverall = 0;
for ($i = 0; $i < $attempts; $i++) {
foreach ($generator($string) as $letter) {}
$timeOverall = bcadd($timeOverall, bcsub($timeEnd, $timeStart)); }
return bcdiv($timeOverall, $attempts); }
function doBenchmark($startLength, $endLength, $step) {
$string = 'å';
for ($length = $startLength; $length < $endLength; $length += $step) {
echo sprintf("string length: %s\n", $length); echo sprintf("preg_split: %s\n", getAverageDurationFor
('letterSequencePregSplit', str_repeat($string, $length))); echo sprintf("mb_substr: %s\n\n", getAverageDurationFor
('letterSequenceMbSubstr', str_repeat($string, $length))); }
}
doBenchmark(1, 500, 50);
PD9waHAKCmJjc2NhbGUoMTUpOwoKZnVuY3Rpb24gbGV0dGVyU2VxdWVuY2VQcmVnU3BsaXQoJHN0cmluZykgewogICAgJHN0cmluZ0FzQXJyYXkgPSBwcmVnX3NwbGl0KCcvL3UnLCAkc3RyaW5nLCBudWxsLCBQUkVHX1NQTElUX05PX0VNUFRZKTsKCSRhcnJheUNvdW50ID0gY291bnQoJHN0cmluZ0FzQXJyYXkpOwoJCiAgICBmb3IgKCRpID0gMDsgJGkgPCAkYXJyYXlDb3VudDsgJGkrKykgewogICAgICAgIHlpZWxkICRzdHJpbmdBc0FycmF5WyRpXTsKICAgIH0KfQoKZnVuY3Rpb24gbGV0dGVyU2VxdWVuY2VNYlN1YnN0cigkc3RyaW5nKSB7CiAgICAkc3RyaW5nTGVuZ3RoID0gbWJfc3RybGVuKCRzdHJpbmcpOwoKICAgIGZvciAoJGkgPSAwOyAkaSA8ICRzdHJpbmdMZW5ndGg7ICRpKyspIHsKICAgICAgICB5aWVsZCBtYl9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