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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:18:26+00:00 2026-06-07T01:18:26+00:00

I am just getting into functional programming and trying to understand when a class/property

  • 0

I am just getting into functional programming and trying to understand when a class/property should be made mutable.

When working with a significant number of string concatenations, we know that it’s better to use StringBuilder, for instance:

using System;
using System.Diagnostics;
using System.Text;

namespace ConsoleApplication3
{
    internal class Program
    {
        private static string myStr;
        private static readonly StringBuilder mySb = new StringBuilder();

        private static void Main(string[] args)
        {
            Profile("+", 100000, () => myStr = myStr + "a"); // Takes 2236 ms
            Profile("SB", 100000, () => mySb.Append("a")); // Takes 1 ms
        }

        private static void Profile(string description, int iterations, Action func)
        {
            // clean up
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            // warm up 
            func();

            Stopwatch watch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                func();
            }
            watch.Stop();
            Console.Write(description);
            Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds);
        }
    }
}

This is the commonly known case in which it’s significantly more performant to concatenate strings via StringBuilder versus the + operator. My assumption is that StringBuilder achieves better performance by creating fewer strings.

Is there a balance between performance and immutability, or is this case an exception for some reason?

  • 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-06-07T01:18:28+00:00Added an answer on June 7, 2026 at 1:18 am

    Joining strings efficiently is less about mutable vs immutable structures, and more about picking the right data structure and evaluation strategy to support O(1) append.

    Typically, trees of various sorts are used to support fast append, which maximise sharing, and minimize copying. Example structures include ropes and finger trees.

    Lazy evaluation can also help in some cases (e.g. if concatenation involves copying, that can be delayed to if and when the tail of the string is actually needed). A strict data structure might incur additional copying overhead in such a case, doing more work than is necessary.

    In your case, I suspect + involves a strict copy of the arguments (i.e. O(n+m)) work, while the string builder can avoid some of the work by amortizing the reallocation of the string buffer (giving you tree-like performance, at the cost of requiring linear use of the structure, and losing thread safety).

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

Sidebar

Related Questions

I'm just getting into functional programming and i'm in the try out some non-trivial
I'm just getting into JavaScript and I'm trying to wrap my head around prototypal
I am just getting back into programming, so forgive me if this is very
I'm just getting into Javascript and I've run into the same problem a number
I'm just barely getting into NodeJS a bit and have hit a snag trying
My little brother is just getting into programming, and for his Science Fair project,
Just getting into SQLite and I understand it does not use datatypes the same
Just getting into the NoSQL stuff so forgive me if this is a simple
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
I am just getting into javascript and d3.js. Please excuse the simple question, but

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.