Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
    
It would be interesting to know why you even need this. I'm assuming that you use something like 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
    
@tim, I use preg_match and build the URL later on since it has to be preg_replaced earlier. – sitilge Dec 23 '15 at 13:06

Which version to use

I wouldn't care about the timing difference. There may be situations where it matters, but in almost all cases, both variants will be so fast that the difference becomes irrelevant (we are talking about a fraction of a millisecond here) and other factors should decide which version you use.

In this case, those other factors might be readability and reusability/adaptability. I think the second version is more readable, but as you said, the first version can be easily adapted.

So if you predict that you need to perform additional changes to the segments, go with the first approach, otherwise with the second.

Differences

Please note that the versions are not the same (the first one only trims slashes, while the second one removes them all). For your use-case, this doesn't matter.

But as this code will probably be wrapped in a function which may then be reused in different contexts (you could eg pass the slash as argument, and then use the function to trim whitespace from arrays (for the first version), or you could use it to remove an unwanted character from the segments (for the second version)), it still matters.

share|improve this answer
    
yes, exactly. The miliseconds can become seconds if performed 1m times, but that seems to be out-of-the-world experience. Will stick to first version one anyway. – sitilge Dec 23 '15 at 11:34

The advantage over using array_map is that for example if you want to manipulate an url before trimming it you can define a function and just pass it to array_map. Otherwise there is no advantage

share|improve this answer
    
Yes, that was what I already mentioned. – sitilge Dec 23 '15 at 9:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.