Last active
August 29, 2015 14:24
-
-
Save neclimdul/33ff53aa26d42abec72e to your computer and use it in GitHub Desktop.
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 | |
$path = 'd'; | |
echo "RouteProvider::getRoutesByPath():\n"; | |
bench('d'); | |
bench('d/d'); | |
bench('d/d/d'); | |
bench('d//d///d'); | |
bench('/d/d/d/d////d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d'); | |
bench('/d/d/d/d///////////////////////////////////////////d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d'); | |
bench(implode('/', array_fill(0, 256, 'd'))); | |
function bench($path) { | |
echo "Path: $path\n"; | |
$runcount = 100000; | |
$parts = array_values(array_filter(explode('/', $path), function($value) { | |
return $value !== NULL && $value !== ''; | |
})); | |
$part_count = count($parts); | |
$t0 = microtime(true); | |
for ($i = 0; $i < $runcount; ++$i) { | |
$npath = trim(preg_replace('#//+#', '/', $path), '/'); | |
$parts = explode('/', $npath); | |
if (count($parts) != $part_count) { | |
echo "Fail\n"; | |
} | |
} | |
$t1 = microtime(true); | |
echo "preg_replace and trim: " . | |
round(($t1 - $t0) * 1000, 2) . | |
"\n"; | |
for ($i = 0; $i < $runcount; ++$i) { | |
if (strlen($path) > 512) { | |
// don't even try... this takes forever. | |
continue; | |
} | |
$parts = array_values(array_filter(explode('/', $path), function($value) { | |
return $value !== NULL && $value !== ''; | |
})); | |
if (count($parts) != $part_count) { | |
echo "Fail\n"; | |
} | |
} | |
$t2 = microtime(true); | |
echo "Closure: " . | |
round(($t2 - $t1) * 1000, 2) . | |
"\n"; | |
for ($i = 0; $i < $runcount; ++$i) { | |
$parts = array(); | |
foreach (explode('/', $path) as $value) { | |
if ($value !== NULL && $value !== '') { | |
$parts[] = $value; | |
} | |
} | |
if (count($parts) != $part_count) { | |
echo "Fail\n"; | |
} | |
} | |
$t3 = microtime(true); | |
echo "Loop: " . | |
round(($t3 - $t2) * 1000, 2) . | |
"\n"; | |
for ($i = 0; $i < $runcount; ++$i) { | |
$parts = preg_split('@/+@', $path, NULL, PREG_SPLIT_NO_EMPTY); | |
if (count($parts) != $part_count) { | |
echo "Fail\n"; | |
} | |
} | |
$t4 = microtime(true); | |
echo "preg_split: " . | |
round(($t4 - $t3) * 1000, 2) . | |
"\n"; | |
echo "\n"; | |
} |
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
RouteProvider::getRoutesByPath(): | |
Path: d | |
preg_replace and trim: 218.09 | |
Closure: 458.16 | |
Loop: 132.25 | |
preg_split: 124.09 | |
Path: d/d | |
preg_replace and trim: 223.98 | |
Closure: 644.98 | |
Loop: 158.73 | |
preg_split: 142.93 | |
Path: d/d/d | |
preg_replace and trim: 240.48 | |
Closure: 848.43 | |
Loop: 177.32 | |
preg_split: 152.93 | |
Path: d//d///d | |
preg_replace and trim: 256.08 | |
Closure: 1439.38 | |
Loop: 224.8 | |
preg_split: 152.36 | |
Path: /d/d/d/d////d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d | |
preg_replace and trim: 415.87 | |
Closure: 5412.93 | |
Loop: 654.75 | |
preg_split: 414.18 | |
Path: /d/d/d/d///////////////////////////////////////////d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d | |
preg_replace and trim: 404.83 | |
Closure: 12547.14 | |
Loop: 1173.48 | |
preg_split: 388.63 | |
Path: d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d/d | |
preg_replace and trim: 2314.46 | |
Closure: 51363.03 | |
Loop: 5599.04 | |
preg_split: 3265.59 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment