я бы чуть ускорил, убрав лишнее
<?php
$string = '34956bbb22644e8ab7112755254fb44d';
function split1($string)
{
$components = [
substr($string, 0, 8),
substr($string, 8, 4),
substr($string, 12, 4),
substr($string, 16, 4),
substr($string, 20)
];
return implode('-', $components);
}
function split2($string)
{
return substr($string, 0, 8) . '-' .
substr($string, 8, 4) . '-' .
substr($string, 12, 4) . '-' .
substr($string, 16, 4) . '-' .
substr($string, 20);
}
function benchmark($max, $func, $string)
{
$timer = [
'start' => microtime(true),
'stop' => false
];
$i = 0;
do {
$i++;
$func($string);
} while ($i < $max);
$timer['stop'] = microtime(true);
printf("benchmark %s %s seconds\n", $func, $timer['stop'] - $timer['start']);
}
benchmark(10e6, 'split1', $string);
benchmark(10e6, 'split2', $string);
benchmark split1 6.28813123703 seconds
benchmark split2 5.1870620250702 seconds