Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Has StringBuilder gotten more efficient in VS2010?

Has StringBuilder gotten more efficient in VS2010?

Scheduled Pinned Locked Moved .NET (Core and Framework)
testingbeta-testingperformancequestioncode-review
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jesarg
    wrote on last edited by
    #1

    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.

    S S 2 Replies Last reply
    0
    • J jesarg

      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.

      S Offline
      S Offline
      so_soul
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • J jesarg

        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.

        S Offline
        S Offline
        Stohn
        wrote on last edited by
        #3

        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.

        J 1 Reply Last reply
        0
        • S so_soul

          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

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • S Stohn

            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.

            J Offline
            J Offline
            jesarg
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups