Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:25:48+00:00 2026-05-11T11:25:48+00:00

I was wondering about StringBuilder and I’ve got a question that I was hoping

  • 0

I was wondering about StringBuilder and I’ve got a question that I was hoping the community would be able to explain.

Let’s just forget about code readability, which of these is faster and why?

StringBuilder.Append:

StringBuilder sb = new StringBuilder(); sb.Append(string1); sb.Append('----'); sb.Append(string2); 

StringBuilder.AppendFormat:

StringBuilder sb = new StringBuilder(); sb.AppendFormat('{0}----{1}',string1,string2); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:25:49+00:00Added an answer on May 11, 2026 at 11:25 am

    It’s impossible to say, not knowing the size of string1 and string2.

    With the call to AppendFormat, it will preallocate the buffer just once given the length of the format string and the strings that will be inserted and then concatenate everything and insert it into the buffer. For very large strings, this will be advantageous over separate calls to Append which might cause the buffer to expand multiple times.

    However, the three calls to Append might or might not trigger growth of the buffer and that check is performed each call. If the strings are small enough and no buffer expansion is triggered, then it will be faster than the call to AppendFormat because it won’t have to parse the format string to figure out where to do the replacements.

    More data is needed for a definitive answer

    It should be noted that there is little discussion of using the static Concat method on the String class (Jon’s answer using AppendWithCapacity reminded me of this). His test results show that to be the best case (assuming you don’t have to take advantage of specific format specifier). String.Concat does the same thing in that it will predetermine the length of the strings to concatenate and preallocate the buffer (with slightly more overhead due to looping constructs through the parameters). It’s performance is going to be comparable to Jon’s AppendWithCapacity method.

    Or, just the plain addition operator, since it compiles to a call to String.Concat anyways, with the caveat that all of the additions are in the same expression:

    // One call to String.Concat. string result = a + b + c; 

    NOT

    // Two calls to String.Concat. string result = a + b; result = result + c; 

    For all those putting up test code

    You need to run your test cases in separate runs (or at the least, perform a GC between the measuring of separate test runs). The reason for this is that if you do say, 1,000,000 runs, creating a new StringBuilder in each iteration of the loop for one test, and then you run the next test that loops the same number of times, creating an additional 1,000,000 StringBuilder instances, the GC will more than likely step in during the second test and hinder its timing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Maybe you don't need a regex? String url = 'http://localhost:8080/MenuTest/index.action';… May 11, 2026 at 1:58 pm
  • added an answer Upon first reading your question, i was under the assumption… May 11, 2026 at 1:58 pm
  • added an answer Action helpers may help you http://devzone.zend.com/article/3350-Action-Helpers-in-Zend-Framework May 11, 2026 at 1:58 pm

Related Questions

I was wondering about the best practices for indicating invalid argument combinations in Python.
I was wondering about the best way to manage projects dependencies from ant. What
I was wondering about what makes the primary Java compiler (javac by sun) so
This may seem like a daft question, but i was wondering about how to
I was always wondering about this seemingly utopic world of open source. Assuming the
I was wondering about StringBuilder and I've got a question that I was hoping
I'm writing the RenderContents() method of my ASP.NET server control. The method uses an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.