Skip to content

Instantly share code, notes, and snippets.

@vlakoff
Last active October 10, 2015 21:58
Show Gist options
  • Save vlakoff/3757671 to your computer and use it in GitHub Desktop.
Save vlakoff/3757671 to your computer and use it in GitHub Desktop.
Optimizing character_limiter() of CodeIgniter's text helper
// https://github.com/bcit-ci/CodeIgniter/pull/1750
$nb = 1000;
$str = str_repeat('<lorem ipsum here>' . "\n", 10);
$t1 = microtime(true);
for ($i = $nb; $i--; ) {
preg_replace('/\s+/', ' ', $str);
}
$t2 = microtime(true);
for ($i = $nb; $i--; ) {
preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str));
}
$t3 = microtime(true);
echo $t2 - $t1;
echo '<br>';
echo $t3 - $t2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment