Are there any advantages on using array_map
& trim
vs using simple str_replace
? The input for both is an array of matched URL segments. I find it nice to be able to provide extra logic when using array_map
but seems to be ~2 times slower. I did some trivial benchmarking
for ($i = 0; $i < 10000; $i ++) {
array_map(function($argument) {
return trim($argument, '/');
}, array_slice($match, 1));
}
$end_time = microtime(TRUE);
echo $end_time - $start_time;
echo "<br/>";
$start_time = microtime(TRUE);
for ($i = 0; $i < 10000; $i ++) {
str_replace('/', '', array_slice($match, 1));
}
$end_time = microtime(TRUE);
echo $end_time - $start_time;
Outputs:
0.0675988197327
0.0296301841736
explode
to get the URL segments, in which case there shouldn't even be any trailing slashes. Knowing the complete context, there might be nicer and faster ways of achieving what you want. – tim Dec 23 '15 at 12:53