2
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 23, 2015 at 12:53
  • \$\begingroup\$ @tim, I use preg_match and build the URL later on since it has to be preg_replaced earlier. \$\endgroup\$ Commented Dec 23, 2015 at 13:06

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Dec 23, 2015 at 11:34
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Yes, that was what I already mentioned. \$\endgroup\$ Commented Dec 23, 2015 at 9:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.