Hey me again *s* Sorry to be such an annoyance... I do have a comment. Yeah those comparisons are DRASTIC... makes you think there is something funky going on... Well there is.
I went back and looked at the BeforeFirst and AfterLast functions and this is why I think the runtimes were so drastic......
BeforeFirst uses the Left function. Your functions just use the InStr function... so.. It appears to me (even though I don't have the text) that the first accurance of this inBetween is fairly close to the beganing of the loaded string (book). At least a great deal closer to the beganning the end. Since BeforeFirst is: (using your sped up version) :
Code: Select all
Function BeforeFirst(sIn, sFirst)
BeforeFirst = LeftB(sIn, InStrB(1, sIn, sFirst) - 1)
End Function
What happens here is that, yes it first searches forward in the string, but then its like ok...Lets use LeftB and lets start at InStrB(blah) - 1.... Well, the problem with this one big strings is that VB is iterating througgh the string BACKWARDS to get to the Position variable. So in this case since the string is very long it takes a while to do that. Yes, very ineffecient compared to yours which uses just the InStr function... which parses FORWARD through the string. In this case it would of been a better solution to do
Code: Select all
BeforeFirst = Mid(sIn, 1, InStrB(1, sIn, sFirst) )
Forgive me if I have any slight syntax errors as I don't really know ifthere is a MidB or even use VB.. (I'm a C man *L*).
Anyway, my point here? That if the first accurance in your test happened near the END of the string data (book) then the outcome of your tests most likely would of been reversed, your's being the slower one by 20+ seconds. (I am giving you credit that your implementation is faster anyway, but I really don't know).
Probably an even better implentation of a faster algo would get the Length of the string to be searched, and compared the position to .5*fullStringLength and if its lower use the implentation you have now, but if it is higher to use the one that incorporates LeftB. Yes, thats a tad (just a smidgeon) more overhead... but doing it that way would speed up your implentation when strings are near the end.
Ok.. I'll be quiet now