Has StringBuilder gotten more efficient in VS2010?
-
I recently wrote some code to clean up user input by turning all multiple-space spaces into single-space spaces. I tried several different approaches to optimize performance, and, interestingly enough, the stringbuilder is performing better than I remember from past versions. I can't seem to do better than the following no matter how hard I try to optimize.
private StringBuilder builder = new StringBuilder(); public string RemoveExtraSpaces(string inputString) { builder.Clear(); int length = inputString.Length; if (length > 0) { builder.Append(inputString\[0\]); for (int loop1 = 1; loop1 < length; loop1++) if (inputString\[loop1 - 1\] != ' ' || inputString\[loop1\] != ' ') builder.Append(inputString\[loop1\]); } return builder.ToString(); }
Even for very short strings, this solution performs better than other solutions. Edit: Also, for clarification, the RemoveExtraSpaces method is called many times in a row on a bunch of strings for performance testing.
-
I recently wrote some code to clean up user input by turning all multiple-space spaces into single-space spaces. I tried several different approaches to optimize performance, and, interestingly enough, the stringbuilder is performing better than I remember from past versions. I can't seem to do better than the following no matter how hard I try to optimize.
private StringBuilder builder = new StringBuilder(); public string RemoveExtraSpaces(string inputString) { builder.Clear(); int length = inputString.Length; if (length > 0) { builder.Append(inputString\[0\]); for (int loop1 = 1; loop1 < length; loop1++) if (inputString\[loop1 - 1\] != ' ' || inputString\[loop1\] != ' ') builder.Append(inputString\[loop1\]); } return builder.ToString(); }
Even for very short strings, this solution performs better than other solutions. Edit: Also, for clarification, the RemoveExtraSpaces method is called many times in a row on a bunch of strings for performance testing.
The Clear() is a new method for StringBuilder in VS2010. But by observing its IL code we can see it just set this.Length = 0. So we can see that the inner algorithm somewhat not change a lot. StringBuilder is a efficient class, not only in VS 2010, I think it's the class itself rather than the IDE version that makes your solution better.
Dynamsoft
-
I recently wrote some code to clean up user input by turning all multiple-space spaces into single-space spaces. I tried several different approaches to optimize performance, and, interestingly enough, the stringbuilder is performing better than I remember from past versions. I can't seem to do better than the following no matter how hard I try to optimize.
private StringBuilder builder = new StringBuilder(); public string RemoveExtraSpaces(string inputString) { builder.Clear(); int length = inputString.Length; if (length > 0) { builder.Append(inputString\[0\]); for (int loop1 = 1; loop1 < length; loop1++) if (inputString\[loop1 - 1\] != ' ' || inputString\[loop1\] != ' ') builder.Append(inputString\[loop1\]); } return builder.ToString(); }
Even for very short strings, this solution performs better than other solutions. Edit: Also, for clarification, the RemoveExtraSpaces method is called many times in a row on a bunch of strings for performance testing.
-
The Clear() is a new method for StringBuilder in VS2010. But by observing its IL code we can see it just set this.Length = 0. So we can see that the inner algorithm somewhat not change a lot. StringBuilder is a efficient class, not only in VS 2010, I think it's the class itself rather than the IDE version that makes your solution better.
Dynamsoft
so_soul wrote:
The Clear() is a new method for StringBuilder in VS2010
The method is new in .NET Framework 4.0, not Visual Studio 2010. Visual Studio can make use of multiple frameworks and is independent of them.
Failure is not an option; it's the default selection.
-
Curious. What else have you tried? * String.Replace? * RegEx.Replace? * StringBuilder.Replace? * Using an array of characters? Perhaps this will give me a research topic for the day, being slow here.
I tried several variants of using the string concatenation += operator, stringbuilder, and arrays of characters. I expected an array of character approach to be best, but every approach I tried was slightly outperformed by the stringbuilder-based method I posted. I didn't bother using RegEx, as I expected its overhead costs to eliminate itself here, but I could be wrong.