Count Instances Of String Within String

  • Thread starter Thread starter Derek Hart
  • Start date Start date
It would be interresting to se how you did the test. When I test it I
get the result that IndexOf is slightly faster, but they never differ
more than a few percent.

Here is what I tested:


HighResolutionClock clock;
int pos;
double time1, time2;
string text, find;

text = "askdjf iuqwh peha sduuhböaos9 döqown eiluhas9ödhföoasid öfoiajsd
fä0sd föoiqwe fuh asilduhfasudh föoiqiweöf oihas dlifyg asliudhf
öoasihdf h";
find = "duhf";

clock = new HighResolutionClock();

clock.Reset();
for (int i = 0; i < 1000000; i++) pos = Strings.InStr(2, text, find,
CompareMethod.Text);
time1 = clock.Seconds;

clock.Reset();
for (int i = 0; i < 1000000; i++) pos = text.IndexOf(find, 1,
StringComparison.CurrentCultureIgnoreCase);
time2 = clock.Seconds;


Typical result:

time1: 3.589
time2: 3.519


(I can post the HighResolutionClock class if you want. It uses the
QueryPerformanceCounter method in kernel32.dll.)
 
jayeldee said:
Is the RegEx.Escape( ) there to account for any characters in your
toFind variable that are RegEx operators?

Exactly. Although a typical word wouldn't contain any, it keeps the
regular expression from crashing if it would.
 
If you have come to that conclusion I think that you haven't used them
in the same way, as both internally calls
CurrentCulture.CompareInfo.IndexOf. InStr only has more overhead.

A slight correction, by default InStr uses InvariantCulture, while
String.IndexOf always uses CurrentCulture.
 
david said:
A slight correction, by default InStr uses InvariantCulture, while
String.IndexOf always uses CurrentCulture.

Yes, if you do a binary search it will use InvariantCulture. So will
IndexOf.
 
Back
Top