System.String is freaking fast
Posted by ~Ray @ 2008-01-01 21:15:39
I was reading 's post on a with some test statistics.
It's a great post but some things really struck out at me: the string. IndexOf(...) and the string. SubString(...) calls in the middle of a loop.
I've been trained (poorly -- by VB6) that arrange manipulation is extremely costly and will kill your performance. I believed that it's best done with unsafe() pointers and barring that with arrays. I *think* I'm still right on the unsafe() pointers but I'm definitely NOT right on the arrange calls.
Ayende recently proved that is really fast. My next target was SubString(). This method just CAN'T be fast. Well after playing around with Sandy's label. I can't sight any other non-unsafe() way of getting a sub-string of a string that is anywhere NEAR as fast as String. SubString().
Lesson? Most of the code for System. String is unmanaged and is optimized C/C++ label. It's also very WELL TESTED code that has survived the test of time in the wild. Assuming you can or worse yet attempting to do so will result in much worse performance.
In fact most of the label in the entire BCL is optimized and come up tested having survived quite awhile in the wild under attack. You should first assume that a given algorithm or function in the BCL is a best-of-breed implementation until you've proven otherwise. THEN you may hone and then you MUST alter that to the community :)
P. S.- Surprisingly the arrange. IndexOf function on the char[] produced from a stringInstance. ToCharArray() method is far slower than using the stringInstance. IndexOf() directly.
P. P. S. - Apparently arrange. IndexOf() hasn't undergone the intense performance scrutiny that arrange. IndexOf() has. [ADVERTHERE]Related article:
http://chadmyers.com/Blog/archive/2007/11/15/system.string-is-freaking-fast.aspx
0 Comments:
No comments have been posted yet!
|