Which method is better to remove a char from a string?
>
A replace or a substring?
Considering that first char is at 0 position. Using reflector, replace
let lost its track in internal call, while substring show what it
really does, so I'm not able to evaluate the efficiency.
Any suggestion?
When you see an [MethodImpl(MethodImplOptions.InternalCall] attribute on
a .NET framework method in the Reflector, that means that the method is implemented
inside the runtime itself -- usually to make the method faster or access
some internal data structure. If you want to see how these methods are likely
implemented, check out the Shared Source CLI from
http://msdn.microsoft.com/net/sscli.
String.Replace(Char, Char) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::Replace
String.Replace(String, String) : String is implemented at sscli\clr\src\vm\comstring.cpp\COMString::ReplaceS tring
Looking at this source, while interesting, won't really give you a sense
of efficiency. First of all, it's not necessarily how the .NET framework
commercial distribution is implemented under-the-hood. And secondly, comparing
un-JITted managed code against native code is pretty futile.
A good way to test efficiency is to create a small test bench that executes
each method several times and compares the timings. If you are using .NET
2.0, you can use the System.Diagnostics.Stopwatch to time the methods with
fair accuracy. Be sure to take several samples and average the timings so
that you can get more accurate readings as other processes running on your
system and the garbage collector may intrude on your benchmarking.
Best Regards,
Dustin Campbell
Developer Express Inc.