String field wins with both Debug on and off on my machine:
Debug on:
StringField: 203
FindString: 219
Debug off:
StringField: 188
FindString: 203
This raises the question: Is it that StringField is just so fast that even with the overhead of multiple passes it wins? Or is it keeping state between calls?
So I tried updating the the loops so that the StringField had to switch between 2 different strings and delimters. This did not change a thing. StringField was still faster.
So this leaves the conclusion that StringField is one damn fine piece of well optimised code.
The question remains, however. Is it just so much faster it can afford to keep going back to the beginning or is it using some very fancy cacheing?
Code: Select all
For a=1 To 60000
b_string$=b_string$+Chr(97+Random(25))
Next
For a=1 To 60000
c_string$=c_string$+Chr(97+Random(25))
Next
Delay(5000)
oldtime=GetTickCount_()
b_max = CountString(b_string$,"b")
c_max = CountString(c_string$,"c")
If b_max > c_max
max = b_max
Else
max = c_max
EndIf
For i=1 To max
temp$ = StringField(b_string$, i, "b")
temp$ + StringField(c_string$, i, "c")
;Debug temp$
Next
newtime=GetTickCount_()
b_Start = 1
c_start = 1
b_Finished = #False
c_Finished = #False
Repeat
If b_Finished = #False
Stop=FindString(b_string$,"b",b_Start)
temp$ = Mid(b_string$,b_Start,Stop - b_Start)
;Debug temp$
b_Start=Stop+1
EndIf
If Stop = 0
b_Finished = #True
EndIf
If c_Finished = #False
Stop=FindString(c_string$,"c",c_start)
temp$ + Mid(c_string$,c_start,Stop-c_start)
;Debug temp$
c_start=Stop+1
EndIf
If Stop = 0
c_Finished = #True
EndIf
Until b_Finished And c_Finished
newtime2=GetTickCount_()
answer$="Speed test using a string with 60000 characters"+Chr(13)+Chr(10)+"Using StringField: "+Str(newtime-oldtime)+Chr(13)+Chr(10)+"Using FindString: "+Str(newtime2-newtime)
MessageRequester("Results",answer$,0)