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 6789677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:37:59+00:00 2026-05-26T17:37:59+00:00

Why is StringBuilder slower when compared to + concatenation? StringBuilder was meant to avoid

  • 0

Why is StringBuilder slower when compared to + concatenation?
StringBuilder was meant to avoid extra object creation, but why does it penalize performance?

    static void Main(string[] args)
    {
        int max = 1000000;
        for (int times = 0; times < 5; times++)
        {
            Console.WriteLine("\ntime: {0}", (times+1).ToString());
            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < max; i++)
            {
                string msg = "Your total is ";
                msg += "$500 ";
                msg += DateTime.Now;
            }
            sw.Stop();
            Console.WriteLine("String +\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));

            sw = Stopwatch.StartNew();
            for (int j = 0; j < max; j++)
            {
                StringBuilder msg = new StringBuilder();
                msg.Append("Your total is ");
                msg.Append("$500 ");
                msg.Append(DateTime.Now);
            }
            sw.Stop();
            Console.WriteLine("StringBuilder\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
        }
        Console.Read();
    }

enter image description here

EDIT: Moving out of scope variables as suggested:

enter image description here

  • 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-26T17:38:00+00:00Added an answer on May 26, 2026 at 5:38 pm

    Change so that the StringBuilder isn’t instantiated all the time, instead .Clear() it:

    time: 1
    String +    :   3348ms
    StringBuilder   :   3151ms
    
    time: 2
    String +    :   3346ms
    StringBuilder   :   3050ms
    

    etc.

    Note that this still tests exactly the same functionality, but tries to reuse resources a bit smarter.

    Code: (also live on http://ideone.com/YuaqY)

    using System;
    using System.Text;
    using System.Diagnostics;
    
    public class Program
    {
        static void Main(string[] args)
        {
            int max = 1000000;
            for (int times = 0; times < 5; times++)
            {
                {
                    Console.WriteLine("\ntime: {0}", (times+1).ToString());
                    Stopwatch sw = Stopwatch.StartNew();
                    for (int i = 0; i < max; i++)
                    {
                        string msg = "Your total is ";
                        msg += "$500 ";
                        msg += DateTime.Now;
                    }
                    sw.Stop();
                    Console.WriteLine("String +\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
                }
    
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    StringBuilder msg = new StringBuilder();
                    for (int j = 0; j < max; j++)
                    {
                        msg.Clear();
                        msg.Append("Your total is ");
                        msg.Append("$500 ");
                        msg.Append(DateTime.Now);
                    }
                    sw.Stop();
                    Console.WriteLine("StringBuilder\t: {0}ms", ((int)sw.ElapsedMilliseconds).ToString().PadLeft(6));
                }
            }
            Console.Read();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started using StringBuilder in preference to straight concatenation, but it seems like it's
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I know you should use a StringBuilder when concatenating strings but I was just
I want to add my own member to the StringBuilder class, but when I
Does a StringBuilder variable need to be initialized to lets say string.empty in case
I have a Stringbuilder object that has been populated from a text file. How
StringBuilder displayList = new StringBuilder(); I have added a few items in displayList but
StringBuilder sb = new StringBuilder(); // Send all output to the Appendable object sb
How the StringBuilder class is implemented? Does it internally create new string objects each

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.