Speed up string comparison,加快比较字符串

本文探讨了VB.NET中字符串比较的效率问题,特别是在不同文化设置下进行本地化比较时。由于需要转换Unicode字符到数值来反映当前文化设置下的字母顺序,这种比较方式比VB6慢。为提高效率,建议使用不区分本地化的CompareOrdinal方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

转载于:https://www.cnblogs.com/josephshi/archive/2008/01/15/1039956.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值