<?php

$subj = str_repeat(' ', 1 << 20);

$t = microtime(true);
for ($i = 0; $i < 100; ++$i) {
	str_replace($subj, '  ', ' ');
}
echo (microtime(true) - $t) . PHP_EOL;

$t = microtime(true);
for ($i = 0; $i < 100; ++$i) {
	preg_replace('/\s{2}/', ' ', $subj);
}
echo (microtime(true) - $t) . PHP_EOL;