in reply to "Capturing the nth word in a string" Algorithm Analysis
Here is the reason why my approach was a 'touch' slower than Grantm's, but both are much faster than others:
Say both of us are trying to match the 3rd word, his regexp only matches the 3rd word, but for the first word and the second word, there is no matching done (to be more precise, no extra effort to make sure backref can happen); But in my case, I am matching all 3 words up to the 3rd one. Although I end up with having the third word stored in $1 just as his does, extra effort are taken to handle the 1st and 2nd words, to make sure the backref for the immediate next one works.
But in both cases (mine and Grantm's), the effort is only up to the 3rd word, not like in some other cases, the matching continues to the end of the string.
Performance (timing) is just one benefit gained here, the other benefit is we used less space, as we don't create an array to hold all words.
Now let's play a little bit math. Remember you mentioned the O and O/2 observation?
Assume we have a string formed up with m words, and the effort to matching one word is n, in the testing you designed:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: "Capturing the nth word in a string" Algorithm Analysis
by Roger (Parson) on Oct 15, 2003 at 02:37 UTC |