Created
November 21, 2012 00:23
-
-
Save fruit/4122239 to your computer and use it in GitHub Desktop.
Comparing str_replace versus preg_quote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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