.NET String.Replace() performance uncovered

In a previous entry I ran into some unexpected results with the performance of String.Replace() in .Net Framework 1.1. Upon further digging, it turns out that Replace is only fast when the input string does not contain the string (or character) that is intended for replacement. When the string does contain it, the performance of CleanString class drops, and, as expected, the character array exhibits better perf. So here's an updated bit of code that behaves no worse than original when there is nothing to replace and better than original when there is cleaning to do:

        public static string CleanString(string source)
        {
            // new result string is only allocated if it is needed
            // otherwise, the method only does a simple scan
            char[] result = null;

            int resultIndex = 0;
            foreach (char c in source)
            {
                if (IsBadCharacter(c))
                {
                    if (null == result)
                    {
                        result = new string(' ', source.Length).ToCharArray();
                    }
                }
                else
                {
                    if (null != result)
                    {
                        result[resultIndex] = c;
                    }
                    resultIndex++;
                }
            }

            return (null == result) ? source : new string(result).Trim();
        }

        private static bool IsBadCharacter(char c)
        {
            return char.IsDigit(c);
        }

posted @ Monday, June 14, 2004 9:43 PM

Print
Comments have been closed on this topic.