MidFast() is faster than Mid() in general use and gets thousand times faster with very large texts (try #TestLen=2500, #TestCount=1): The longer the text, the faster MidFast() gets...
#TestLen = 10
#TestCount = 10000
Define Text.s, C.s, i, iMax
For i = 1 To #TestLen
Text + "testTesttEsttestTESTtesttestteSt"
Next
ts1 = ElapsedMilliseconds()
For j = 1 To #TestCount
i = 0
iMax = Len(Text)
Repeat
i + 1
C = Mid(Text, i, 1)
Until i >= iMax
Next
te1 = ElapsedMilliseconds()
Macro MidFast(String, StartPos, Length)
PeekS(@String + ((StartPos - 1) * SizeOf(Character)), Length)
EndMacro
ts2 = ElapsedMilliseconds()
For j = 1 To #TestCount
i = 0
iMax = Len(Text)
Repeat
i + 1
C = MidFast(Text, i, 1)
Until i >= iMax
Next
te2 = ElapsedMilliseconds()
MessageRequester("Result", "Mid() - " + Str(te1 - ts1) + #CRLF$ + "MidFast() - " + Str(te2 - ts2))
Edit:
Trond notes that it's just faster because this method doesn't check for valid position/length, so keep that in mind...
Last edited by c4s on Thu Feb 03, 2011 8:06 pm, edited 1 time in total.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
Well, this MidFast() is the same or slower than Mid() on my x86 system with the optional parameters included?
No debugger and either unicode=ON or OFF.
skywalk wrote:Well, this MidFast() is the same or slower than Mid() on my x86 system with the optional parameters included?
No debugger and either unicode=ON or OFF.
Yes me too, on my Linux Ubuntu x64. But when I change the "Length" to 1 instead of -1 The MidFast() it's much faster than Mid() function.
Thanks STARGÅTE!
MidFast() is only slower when the Length = -1, not sure why, but it is worth the extra speed for other cases.
This now makes sense to use.
PureBasic does this automatically so the mode parameter isn't needed at all and if it's slower with "-1" for length then better don't use it, so that it will remind you.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
#TestLen = 10
#TestCount = 1000
Define Text.s, C.s, C1.s, i, iMax
For i = 1 To #TestLen
Text + "testTesttEsttestTESTtesttestteSt"
Next
ts1 = ElapsedMilliseconds()
For j = 1 To #TestCount
i = 0
iMax = Len(Text)
Repeat
i + 1
C1 = Mid(Text, i)
Until i >= iMax
Next
te1 = ElapsedMilliseconds()
CompilerIf #PB_Compiler_Unicode
Macro MidFast(String, StartPos, Length=-1)
PeekS(@String + ((StartPos - 1) * SizeOf(Character)), Length, #PB_Unicode)
EndMacro
CompilerElse
Macro MidFast(String, StartPos, Length=-1)
PeekS(@String + ((StartPos - 1) * SizeOf(Character)), Length, #PB_Ascii)
EndMacro
CompilerEndIf
ts2 = ElapsedMilliseconds()
For j = 1 To #TestCount
i = 0
iMax = Len(Text)
Repeat
i + 1
C = MidFast(Text, i)
Until i >= iMax
Next
te2 = ElapsedMilliseconds()
MessageRequester("Result", "Mid() - " + Str(te1 - ts1) + #CRLF$ + "MidFast() - " + Str(te2 - ts2) + #CRLF$ + #CRLF$ + "Text = C : "+Str(C=C1))
On my PC(Win7 x64) the MidFast was faster on both cases
Result with Length = -1
- Mid() - 468
- MidFast() - 312
Result with Length = 1
- Mid() - 102
- MidFast() - 31
Sorry by bad English.
HP Pavilion DV6-2155DX: Intel i3-330m 2.13 / 4GB DDR3 / 500GB Sata2 HD / Display 15.6" LED / Win7 Ultimate x64 / PB 4.50 x86 demo.
Just a side note: The reason the normal Mid() is slow is because it does bound checking (so you don't access memory outside the string). To use a Mid() without bound checking safely you would most often need to check the length beforehand, which would nullify the speed gain.
Seems that I did something wrong during my testings, now it works here as well. I edited my first post to not confuse others and also added Trond's note.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!