<?php
define('TEST_LOOPS', 10);
define('TEST_STRING', str_repeat('English+日に本ほん語ご', 16000));
//define('TEST_STRING', str_repeat('English', 16000));
//define('TEST_STRING', str_repeat('日に本ほん語ご', 16000));
error_reporting(-1);
mb_internal_encoding('UTF-8');
function test($label, $callback)
{
$time = microtime(true);
for ($i = 0; $i < TEST_LOOPS; $i++) {
$callback();
}
$duration = microtime(true) - $time;
echo "{$label}: {$duration}s\n";
}
test('preg_split+count', function() {
$chars = preg_split('//u', TEST_STRING, -1, PREG_SPLIT_NO_EMPTY);
$len = count($chars);
for ($i = 0; $i < $len; $i++) {
$char = $chars[$i];
// use $char
}
});
test('preg_split+foreach', function() {
$chars = preg_split('//u', TEST_STRING, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
// use $char
}
});
test('preg_match_all', function() {
if (preg_match_all('/./u', TEST_STRING, $matches)) {
foreach ($matches[0] as $char) {
// use $char
}
}
});
preferences:
23.29 ms | 410 KiB | 5 Q