Skip to content

Instantly share code, notes, and snippets.

@fruit
Created November 21, 2012 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fruit/4122239 to your computer and use it in GitHub Desktop.
Save fruit/4122239 to your computer and use it in GitHub Desktop.
Comparing str_replace versus preg_quote
<?php
$namespace = 'Doctrine\Common\Annotations\Annotation\Attribute';
$it = 5;
$c = 1000000;
sleep(3);
$tm = microtime(1);
$sum = 0;
for ($t = 0; $t < $it; $t++)
{
$m = microtime(1);
for ($i = 0; $i < $c; $i++) $val = str_replace('\\', '\\\\', $namespace);
printf("Test %2d: %.4f\n", $t + 1, $run = microtime(1) - $m);
$sum += $run;
}
printf("Elapsed (str_replace): %.4f (avg: %.4f)\n", microtime(1) - $tm, $sum / $it);
echo PHP_EOL;
$tm = microtime(1);
$sum = 0;
for ($t = 0; $t < $it; $t++)
{
$m = microtime(1);
for ($i = 0; $i < $c; $i++) $val = preg_quote($namespace);
printf("Test %2d: %.4f\n", $t + 1, $run = microtime(1) - $m);
$sum += $run;
}
printf("Elapsed (preg_quote): %.4f (avg: %.4f)\n", microtime(1) - $tm, $sum / $it);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment