in reply to Why is while(m//g){} so much slower than s///eg and what can I do about it?
Now first of all I would take what was the general first recommendation quite seriously - solve this in multiple passes. I would also simplify your logic - do you really need the nested logic?
But if you want to proceed with the second, I would avoid ever using $` etc. If you look closely I did that with Why I like functional programming which I pointed you at before. Using those special variables slows down all REs, and you are going to use a lot of them. Instead I would arrange (as I did) to pass through the string caching everything just once.
Even so a single pass with s///; is going to be much faster than a single pass with this more sophisticated algorithm for a whole ton of reasons. For instance it is looping in C, you are in Perl, so there is a factor of 10 difference right there. (Which is one of the reasons that so many suggested that you go with KISS until you don't have a choice.) But when the logic goes beyond what multiple passes can readily handle, or if you are making enough passes that don't do much, then the performance difference will reverse. This is often true. Do something sophisticated and you take an immediate performance hit - but then scale to more complex logic better.
|
|---|