This is a very small part of a very big programme. I’m using string arrays to store lots of info. “B” is used as a space filler. I’m also using “C” as a prefix for calculated values.
BUT…
I have just tested it. Wrote a little prog. Set “Profile Code” on.
Way 1 takes 2 milliseconds, way 2 takes 1 milliseconds. So logic is slower than concat ops.
But not for a single character comparison like this. The overhead of calling the method overwhelms the speed advantage of the byte comparison in my tests.
#pragma BackgroundTasks False
dim arr( 999999 ) as string
for i as integer = 0 to arr.Ubound step 2
arr( i ) = "B"
next
dim msg as string
dim sw as new Stopwatch_MTC
sw.Start
for i as integer = 0 to arr.Ubound
if arr( i ) = "B" then
// Do something
end if
next
sw.Stop
If I substitute the “=” with StrComp, it is slower by about 14%. Mind you, we are talking about 48 ms vs. 55 ms, so it no huge deal, but still slower.
In a compiled app the difference is more dramatic: 113msec vs 30msec.
Are you using the wrong StrComp mode?
My code:
Pushbutton1.action:
#pragma DisableBackgroundTasks
dim t0 as double = Microseconds
dim X as string = "C"
for i as integer = 1 to 1000000
if X = "B" then
end if
next
MsgBox "String equals took " + format((Microseconds-t0)/1000, "#") + "msec"
and
#pragma DisableBackgroundTasks
dim t0 as double = Microseconds
dim X as string = "C"
for i as integer = 1 to 1000000
if strComp(X,"B",0) =0 then
end if
next
MsgBox "StrComp binary took " + format((Microseconds-t0)/1000, "#") + "msec"
As an aside, Beware unnecessary code optimizations that obfuscate otherwise easy to read code. We’re quibbling about an IF condition? If you’ve optimized your code down to where this is your primary concern, my hat is off to you!
I get the same results. However, if I change the value of X to “B”, the “=” version takes half the time as StrComp, and that explains the discrepancy between my results and yours. My code was only matching half the time.
In fact, when there is a match, “=” takes about 7 ms and StrComp about 15 ms. With your code as written, it takes 170 ms vs. 28 ms. You’d think that StrComp, at least, would take the same amount of time whether there is a match or not.
How about that?
So the bottom line is, if case matters, you’re better off using StrComp even for a small strings.
The thread topic has drifted (or perhaps “been hijacked”) where I’m focusing more on string comparison performance. I don’t think an almost 5-fold performance in string comparisons is something to ignore, at least not in those cases where the app is primarily doing string-based calculations.
But you’re comparing apples and oranges. By using mode 0 in strcomp, you’ve changed the sense of the comparison and are only handling non-UTF strings. Granted, that may make sense in many cases, but you have introduced a subtle difference and, depending on your goals, skewed the results.