The Compare method compares strings in a local-aware fashion, so it has to convert the Unicode code of each character into a numeric value that reflects its position in the current culture's alphabet. For example, the Compare method considers the "b" lowercase character to come immediately after the "A" uppercase char and before the "B" uppercase char, even though the "A" and "B" characters are contiguous in the Unicode character set. This conversion activity takes time and consume CPU cycles, so you'll find that VB.NET is less efficient than VB6 at comparing strings. Using the = operator and other comparison operators doesn't help at all, because they map to the Compare method behind the scenes, so these operators suffer from the same performance loss.

If you are only interested in checking whether two strings contain the same characters (in a case-sensitive comparison), you can speed up your code by using the CompareOrdinal shared method. This method is 3-4 times faster than the Compare method (or the = operator) because it just scans the two strings and compare the Unicode numeric code of each character.

posted on 2008-01-15 15:52  josephshi  阅读(295)  评论(0编辑  收藏  举报