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

  • SEARCH
  • Home
  • 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 934697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:02:35+00:00 2026-05-15T21:02:35+00:00

Possible Duplicate: String concatenation vs String Builder. Performance Any difference (performance and memory usage)

  • 0

Possible Duplicate:
String concatenation vs String Builder. Performance

Any difference (performance and memory usage) between the following two options?

option 1:

StringBuilder msgEntry = new StringBuilder();
msgEntry.AppendLine("<" + timeTag + ">" + timeStamp + "</" + timeTag + ">");

options 2:

StringBuilder msgEntry = new StringBuilder();
msgEntry.Append("<");
msgEntry.Append(timeTag);
msgEntry.Append(">");
msgEntry.Append(timeStamp);
msgEntry.Append("</");
msgEntry.Append(timeTag );
msgEntry.Append(">\n");
  • 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. Editorial Team
    Editorial Team
    2026-05-15T21:02:35+00:00Added an answer on May 15, 2026 at 9:02 pm

    The second is possibly slightly better in terms of memory use, because it doesn’t need to compute the intermediate string1… but it’s less readable, IMO.

    Personally I’d use:

    msgEntry.AppendFormat("<{0}>{1}</{0}>", timeTag, timeStamp);
    

    You haven’t shown what you want to do with the StringBuilder afterwards. If you’re just going to convert it to a string, then I’d use:

    string text = string.Format("<{0}>{1}</{0}>", timeTag, timeStamp);
    

    to start with.

    What’s the performance like? Well, probably worse – after all, it’s got to parse the format string. But unless you’ve measured this and found it to be the bottleneck, why are you worried?

    In general:

    • Make sure your architecture is reasonably efficient – that’s hard to change later.
    • Balance your internal design between efficiency and simplicity, with an emphasis on testability; changing the design later may take a while, but it should usually be feasible without compatibility issues.
    • Write your implementation to be as readable as possible.
    • Measure the system to find out whether it performs well enough, and where the bottlenecks are. They’re almost never going to be in code like this. (We’re not talking about string concatenation in a loop here, after all.)
    • When you’ve found a bottleneck, try different optimizations and measure them too. Don’t assume that something you think will be faster will actually be faster.

    1 Or the array to pass to Concat… we don’t know the type of timeStamp so we can’t tell exactly what’s going on there; in the second form it may be appended in-place whereas the first form may need to box it and then convert it to a string before performing the concatenation.

    The exact implementation for reallocation etc may well have changed between .NET 3.5 and .NET 4 (I know some bits of the implementation have). Without very careful benchmarking, I’d be really loathe to say which is faster… but the readability is easier to call, albeit subjectively.

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

Sidebar

Related Questions

Possible Duplicate: String formatting options: pros and cons What's the difference between %.2f %
Possible Duplicate: Differences in string compare methods in C# Is there any difference between
Possible Duplicate: Insert string between two points with PHP How can I replace everything
Possible Duplicate: java String concatenation How to Improve performance of this chunk of code
Possible Duplicate: String vs string in C# What is the difference Between String and
Possible Duplicate: String vs string in C# I know there is no difference between
Possible Duplicate: String vs string in C# I understand there is a difference between
Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
Possible Duplicate: JavaScript Variable inside string without concatenation - like PHP In PHP, double
Possible Duplicate: java String concatenation StringBuilder vs String concatenation in toString() in Java I

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.