Does anyone know why C# (.NET)'s StartsWith function is considerably slower than IsPrefix?
相关问题
- Generic Generics in Managed C++
- how to split a list into a given number of sub-lis
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Faster loop: foreach vs some (performance of jsper
I think it's mostly fetching the thread's current culture.
If you change Marc's test to use this form of
String.StartsWith
:it comes a lot closer.
If you use
s1.StartsWith(s2, StringComparison.Ordinal)
it's a lot faster than usingCompareInfo.IsPrefix
(depending on theCompareInfo
of course). On my box the results are (not scientifically):Obviously that's because it's really just comparing 16 bit integers at each point, which is pretty cheap. If you don't want culture-sensitive checking, and performance is particularly important to you, that's the overload I'd use.
Check out the source of IsPrefix. The thing is - in some cases, it's going to be slower than
StartsWith
just because it actually uses StartsWith and does few more operations.StartsWith calls IsPrefix internally. It assigns culture info before calling IsPrefix.
Good question; for a test, I get:
Test rig: