note 83988 added to function.str-replace
| From: | jaimthorn at yahoo dot com | Date: | Sun, 22 Jun 2008 16:16:50 +0000 |
| Subject: | note 83988 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140720@lists.php.net to get a copy of this message | ||
In response to max's comment on June 20th:
Although I agree with the general statements that you made in your posting, I do at the same time
disagree with most of what you wrote.
1) You wrote: Just like "foreach" is slower than "for"
Minor point, but: foreach is NOT slower than for. See:
http://derekgallo.com/2007/04/19/for-vs-foreach/
I ran similar tests myself a while ago, and if memory serves (I posted the results on the company
wiki, so I can forget the details after that :-) the tests showed that the foreach did its job in
35% of the time that a regular for loop took. In my mind this was never a surprise (as it apparently
is to you, and to the author of aforementioned article), because I'm pretty sure PHP
doesn't store its arrays in at consecutive memory addresses, so indexing it through an index
(as one would be, using a vanilla for loop) introduces an overhead that an iterator doesn't
have.
2) You wrote: expanding pieces of code into simpler, faster routines, general redesign, etc.). The
latter one basically turns the code into a mess.
The obvious answer to that is: not if you do it properly. And based on practical experience, I can
even say: on the contrary. BECAUSE I spent more time thinking about optimization of code, my
simpler, faster code is (much) more tidy than the unoptimized version. If it's not, I refrain
from the optimization.
3) You wrote: "Write code for clarity [..] and rather keep the code clearer.
What you wrote seems to amount to a claim that your code is clearer and easier to maintain than
mine; if that's true, I would love to read what metrics you used:
- my code is 20% shorter than yours
- your function body has 5 non-empty lines; mine has 6, two of which only contain a curly bracket
- your code uses create_function and array_map, two functions that I'm CONVINCED far fewer PHP
programmers use than any of the stuff that I used in my code --you'll no-doubt agree that code
complexity is a factor in clarity and maintainability.
Add to all this that
- your code doesn't have any generality that mine lacks
- your code is functionally equivalent to mine
- mine is about 4.5 faster than yours
and I'm pretty sure most programmers would prefer my code over yours. The exception to the
rule would of course be the programmers who use create_function just for the fun of having used it.
Please do note that I agree with the general statements that you made, I just don't think they
apply in this case.
Cheers!
----
Server IP: 213.249.64.162
Probable Submitter: 213.93.24.224
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/83988
Del: integrated -- https://master.php.net/note/delete/83988/integrated
Del: useless -- https://master.php.net/note/delete/83988/useless
Del: bad code -- https://master.php.net/note/delete/83988/bad+code
Del: spam -- https://master.php.net/note/delete/83988/spam
Del: non-english -- https://master.php.net/note/delete/83988/non-english
Del: in docs -- https://master.php.net/note/delete/83988/in+docs
Del: other reasons-- https://master.php.net/note/delete/83988
Reject -- https://master.php.net/note/reject/83988
Search -- https://master.php.net/manage/user-notes.php