Home All Groups Group Topic Archive Search About

System.String Compare vs. IndexOf

Author
7 May 2007 6:52 PM
vladislavf
Hi All,

Briefly:
I need to find all occurrences of an input word in very large array of
strings.
I can not sort it.
For the beginning I used String.Compare (and also CompareTo) . The
elapsed time was pretty good  (the same for both methods): for an
array 1 500 000 words (English) avg. time was about 260 milliseconds.

Then I decided to put all the words into very long string (don't care
about RAM)  and to use IndexOf for search.

The result was surprising : The second approach (IndexOf) is much
faster - 70 milliseconds for the same test.

I guess that IndexOf is faster because its algorithm is more effective
comparing to "Compare" method.
And the second reason is array boundary checkings are not performed .
Am I right  ?Or there are another reason?
Thank you very much!!!
Vladislav

AddThis Social Bookmark Button