My results (using the code by MichaelW on a Pentium I7):
2977 cycles, Instr s1
165 cycles, FindString s1
3269 cycles, Instr s2
1121 cycles, FindString s2
4156 cycles, Instr s3
3946 cycles, FindString s3
The timing is almost the same every time I run the benchmark. Almost no difference between different runs. Very accurate.
I know the rtlib uses strchr for patterns no longer than 1 char (the rtlib uses Boyer Moore otherwise). So I tried a case where the pattern length equals 1.
I added
to the code and put the character 4 before "on" in s3 (so s3 now reads "while expanding 4on"). Tested using instr and FindString.
counter_begin( 1000, HIGH_PRIORITY_CLASS )
instr( 1, s3, ss4 )
counter_end
print counter_cycles, "cycles, Instr ss4"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
FindString( s3, ss4,1 )
counter_end
print counter_cycles, "cycles, FindString ss4"
Results:
722 cycles, Instr ss4
2711 cycles, FindString ss4
So what happens when using another function from the c rtlib, strstr, for string matching?
Benchmark using strstr, instr and FindString.
Code: Select all
dim as string s1,ss1,s2,ss2,s3,ss3,ss4
dim as zstring ptr z
s1 = "FreeBASIC"
ss1 = "IC"
s2 = "FreeBASIC is a free 32-bit compiler for the BASIC language."
ss2 = "language"
s3 = s2 & "It is open source and licensed under the GPL."
s3 &= "It is designed to be syntax compatible with QuickBASIC,"
s3 &= "while expanding 4on the language and capabilities."
ss3 = "language and capabilities"
ss4 = "4"
print s1
print
print s2
print
print s3
print
print instr( s1 , ss1 ),
print FindString( s1, ss1, 1 )
z = strstr( strptr(s1), strptr(ss1))
print (z - strptr(s1)) + 1
print instr( s2 , ss2 ),
print FindString( s2, ss2, 1 )
z = strstr( strptr(s2), strptr(ss2))
print (z - strptr(s2)) + 1
print instr( s3 , ss3 ),
print FindString( s3, ss3, 1 )
z = strstr( strptr(s3), strptr(ss3))
print (z - strptr(s3)) + 1
print instr( s3,ss4 ),
print FindString( s3,ss4, 1)
z = strstr( strptr(s3), strptr(ss4))
print (z - strptr(s3)) + 1
print
sleep 3000
counter_begin( 1000, HIGH_PRIORITY_CLASS )
counter_end
print counter_cycles
counter_begin( 1000, HIGH_PRIORITY_CLASS )
z = strstr( strptr(s1), strptr(ss1))
counter_end
print counter_cycles, "cycles, strstr s1"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
instr( 1, s1, ss1 )
counter_end
print counter_cycles, "cycles, Instr s1"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
FindString( s1, ss1, 1 )
counter_end
print counter_cycles, "cycles, FindString s1"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
strstr( strptr(s2), strptr(ss2))
counter_end
print counter_cycles, "cycles, strstr s2"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
instr( 1, s2, ss2 )
counter_end
print counter_cycles, "cycles, Instr s2"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
FindString( s2, ss2, 1 )
counter_end
print counter_cycles, "cycles, FindString s2"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
strstr( s3, ss3)
counter_end
print counter_cycles, "cycles, strstr s3"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
instr( 1, s3, ss3 )
counter_end
print counter_cycles, "cycles, Instr s3"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
FindString( s3, ss3, 1 )
counter_end
print counter_cycles, "cycles, FindString s3"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
strstr( s3, ss4)
counter_end
print counter_cycles, "cycles, strstr ss4"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
instr( 1, s3, ss4 )
counter_end
print counter_cycles, "cycles, Instr ss4"
counter_begin( 1000, HIGH_PRIORITY_CLASS )
FindString( s3, ss4,1 )
counter_end
print counter_cycles, "cycles, FindString ss4"
sleep
results
8 8
8
51 51
51
184 184
184
176 176
176
0
51 cycles, strstr s1
2971 cycles, Instr s1
166 cycles, FindString s1
349 cycles, strstr s2
3275 cycles, Instr s2
1115 cycles, FindString s2
1109 cycles, strstr s3
4180 cycles, Instr s3
3927 cycles, FindString s3
431 cycles, strstr ss4
727 cycles, Instr ss4
2711 cycles, FindString ss4
Of course strstr is a 'raw' strstr. If any string (either pattern or haystack) equals 0 then strstr crashes. Which is not so good (makes you wonder why the msvcrt does not catch those errors?) And starting the matching at a different position is only possible by doing some pointer arithmetic which means more checking to guard against strstr crashing.
I'm not quite sure how to interpret the results using strstr. Perhaps I'm doing something wrong here. The results of the last testcase seems about right (instr using strchr at 727 and strstr at 431). But the other results are 'suspicious'.