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

The Archive Base Latest Questions

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

I know this question has been done but I have a slightly different twist

  • 0

I know this question has been done but I have a slightly different twist to it. Several have pointed out that this is premature optimization, which is entirely true if I were asking for practicality’s sake and practicality’s sake only. My problem is rooted in a practical problem but I’m still curious nonetheless.


I’m creating a bunch of SQL statements to create a script (as in it will be saved to disk) to recreate a database schema (easily many many hundreds of tables, views, etc.). This means my string concatenation is append-only. StringBuilder, according to MSDN, works by keeping an internal buffer (surely a char[]) and copying string characters into it and reallocating the array as necessary.

However, my code has a lot of repeat strings (“CREATE TABLE [“, “GO\n”, etc.) which means I can take advantage of them being interned but not if I use StringBuilder since they would be copied each time. The only variables are essentially table names and such that already exist as strings in other objects that are already in memory.

So as far as I can tell that after my data is read in and my objects created that hold the schema information then all my string information can be reused by interning, yes?

Assuming that, then wouldn’t a List or LinkedList of strings be faster because they retain pointers to interned strings? Then it’s only one call to String.Concat() for a single memory allocation of the whole string that is exactly the correct length.

A List would have to reallocate string[] of interned pointers and a linked list would have to create nodes and modify pointers, so they aren’t “free” to do but if I’m concatenating many thousands of interned strings then they would seem like they would be more efficient.

Now I suppose I could come up with some heuristic on character counts for each SQL statement & count each type and get a rough idea and pre-set my StringBuilder capacity to avoid reallocating its char[] but I would have to overshoot by a fair margin to reduce the probability of reallocating.

So for this case, which would be fastest to get a single concatenated string:

  • StringBuilder
  • List<string> of interned strings
  • LinkedList<string> of interned strings
  • StringBuilder with a capacity heuristic
  • Something else?

As a separate question (I may not always go to disk) to the above: would a single StreamWriter to an output file be faster yet? Alternatively, use a List or LinkedList then write them to a file from the list instead of first concatenating in memory.

EDIT:
As requested, the reference (.NET 3.5) to MSDN. It says: “New data is appended to the end of the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, then the new data is appended to the new buffer.” That to me means a char[] that is realloced to make it larger (which requires copying old data to the resized array) then appending.

  • 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-11T18:25:37+00:00Added an answer on May 11, 2026 at 6:25 pm

    For your separate question, Win32 has a WriteFileGather function, which could efficiently write a list of (interned) strings to disk – but it would make a notable difference only when being called asynchronously, as the disk write will overshadow all but extremely large concatenations.

    For your main question: unless you are reaching megabytes of script, or tens of thousands of scripts, don’t worry.

    You can expect StringBuilder to double the allocation size on each reallocation. That would mean growing a buffer from 256 bytes to 1MB is just 12 reallocations – quite good, given that your initial estimate was 3 orders of magnitude off the target.

    Purely as an exercise, some estimates: building a buffer of 1MB will sweep roughly 3 MB memory (1MB source, 1MB target, 1MB due to
    copying during realloation).

    A linked list implementation will sweep about 2MB, (and that’s ignoring the 8 byte / object overhead per string reference). So you are saving 1 MB memory reads/writes, compared to a typical memory bandwidth of 10Gbit/s and 1MB L2 cache.)

    Yes, a list implementation is potentially faster, and the difference would matter if your buffers are an order of magnitude larger.

    For the much more common case of small strings, the algorithmic gain is negligible, and easily offset by other factors: the StringBuilder code is likely in the code cache already, and a viable target for microoptimizations. Also, using a string internally means no copy at all if the final string fits the initial buffer.

    Using a linked list will also bring down the reallocation problem from O(number of characters) to O(number of segments) – your list of string references faces the same problem as a string of characters!


    So, IMO the implementation of StringBuilder is the right choice, optimized for the common case, and degrades mostly for unexpectedly large target buffers. I’d expect a list implementation to degrade for very many small segments first, which is actually the extreme kind of scenario StringBuilder is trying to optimize for.

    Still, it would be interesting to see a comparison of the two ideas, and when the list starts to be faster.

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

Sidebar

Ask A Question

Stats

  • Questions 269k
  • Answers 269k
  • 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
  • Editorial Team
    Editorial Team added an answer Here is the syntax for DataView.Sort: private void SortByTwoColumns() {… May 13, 2026 at 1:15 pm
  • Editorial Team
    Editorial Team added an answer $(":input.comment, :input.link").each(function() { alert($(this).val()); // or this.val }); See: http://docs.jquery.com/Utilities/jQuery.each… May 13, 2026 at 1:15 pm
  • Editorial Team
    Editorial Team added an answer I found the answer while looking around in the contacts… May 13, 2026 at 1:15 pm

Related Questions

I know this question has been asked many times, but my problem is a
sorry if this question has been asked before, I searched but wasn't sure on
We have a bit of a messy database situation. Our main back-office system is
I know the question is very subjective. But I cannot form the question in

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.