The Artima Developer Community
Sponsored Link

.NET Buzz Forum
StringBuilders vs. String Concatenation

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Scott Hanselman

Posts: 1031
Nickname: glucopilot
Registered: Aug, 2003

Scott Hanselman is the Chief Architect at Corillian Corporation and the Microsoft RD for Oregon.
StringBuilders vs. String Concatenation Posted: Nov 8, 2003 4:17 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Scott Hanselman.
Original Post: StringBuilders vs. String Concatenation
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Scott Hanselman
Latest Posts From Scott Hanselman's ComputerZen.com

Advertisement

Some folks have said (you've heard them in the halls), “Oh ya, you should NEVER use String Concatenation, dude, use StringBuilder...ALWAYS.  Totally.”

Rico weighs in on this, and he's right on (emphasis mine):

I tend to give advice like "Many users find Stringbuilders useful for their concatenation pattens, consider using them and measure to see if they help you".  Wussy but safe :)

Big string -> small appends
It's substantially likely that appends will fit in the slop and so they're fast, this is the best case (buffer size becomes double the string when it no longer fits so on average the slop is half the current string length) (if there are lots of small appends to a big string you win the most using stringbuilder)

Big string -> big appends
While the string is comparable in size (or smaller) to the appends stringbuilder won't save you much, if this continues to the point where the appends are small compared to the accumlated string you're in the good case

Small string -> big appends
bad case, string builder will just slow you down until enough slop has built up to hold those appends, you move to "big string big appends" as you append and finally to "big string small appends" if/when the buffer becomes collossal

Small string -> small appends
could be ok if you had a good idea how big your string was going to get and preallocated enough so that you have sufficient slop for the appends. You might be able to do better if you just concated all the small appends together in one operation.

It's very hard to say which is faster/smaller in general... it's all about the usage pattern. [Rico Mariani]

Read: StringBuilders vs. String Concatenation

Topic: Killing Cancer with a Virus Previous Topic   Next Topic Topic: Matrix Revolutions Rocks!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use