4

What is more efficient in performance to prepend a string to another?

Using the StringBuilder.Insert method or the string.Concat method?

messageString.Insert(0, prependedString);

or

string.Concat(prependedString, messageString);

In my case the message string is relatively big, the prepended string is short.

12
  • 3
    If you're concating more than two strings use StringBuilder.
    – Bauss
    Sep 25, 2015 at 9:53
  • 2
    @M.kazemAkhgary that is not true. In fact, StringBuilder is preferred for exactly this reason - concatenating strings forces multiple temporary string allocations while a StringBuilder manipulates only a single buffer. Those temporary strings remain until the GC collects them, which results in CPU waste as well. Over time, these allocations can cause performance problems Sep 25, 2015 at 10:00
  • 1
    @ChristophBrückmann there are a lot of articles on the matter. Concatenating two strings creates a third. Concatenating three, creates a fourth. Each of them requires the allocation of a buffer, which stays in memory until the Garbage collector runs. In fact, too many temporary strings can force a Garbage collection. This is annoying in desktop applications but can be very bad in busy server or web applications - the number of zombies adds up and the server has to stop processing and wait for GC to collect them Sep 25, 2015 at 10:03
  • 1
    @M.kazemAkhgary why would anyone keep instantiating a new stringbuilder inside a loop? Sep 25, 2015 at 10:04
  • 1
    Even if background garbage collection is enabled, CPU time is CPU time - even if it runs in another core, it still steals cycles from processing. Sep 25, 2015 at 10:09

2 Answers 2

8

string.Concat is the fastest method if the number of items is fixed. This statement holds true in all cases. It does not matter how long the strings are.

string.Concat calculates the final string size and then copies over the bits into a freshly allocated string. It cannot be done any faster.

In fact, you should write a + b instead of calling Concat (if that is possible in the specific situation).

for huge strings use string builder

False. Why would that be the case?!

If you're concating more than two strings use StringBuilder

False. If the number is fixed, use Concat. StringBuilder gains you nothing but adds overhead.

the answer depends on how many strings you are concatenating, and how big they are

False. The algorithm that I described above is always the fastest possible solution.

The myths around StringBuilder are an amazing variety. If you understand how both options work internally you can answer all these questions yourself. I did not study and memorize all these answers. I generate them from my understanding of internals.

9
  • I agree with everything you've said, but just to avoid any misconceptions, we should use StringBuilder when frequently mutating a string, right? For memory reasons, if nothing else. Sep 25, 2015 at 10:10
  • @Asad you can't mutate a string so I assume you mean a dynamic number of operations on a temporary string. Then, StringBuilder usually is the right choice. I find this to be exceedingly rare in practice. The only case where this is often being done is concatenating a sequence of some sort. We have string.Join for that.
    – usr
    Sep 25, 2015 at 10:11
  • You limited the (very vague) question to a very limited case - not just fixed strings, but the number is known at compile time. Performance isn't only about clock time, or how fast a single operation executes. Sep 25, 2015 at 10:11
  • "Building a string", if you prefer. By string, I mean a string of characters, conceptually, as opposed to the type. As far as use cases go, building large log outputs from the wrong kind of string concatenation often causes bottlenecks. Sep 25, 2015 at 10:12
  • @PanagiotisKanavos in the comments he clarifies that he wants to join just those two strings. Let's wait for the OP to respond.
    – usr
    Sep 25, 2015 at 10:12
3

This is a duplicate of How to use StringBuilder Wisely you can read my full answer over there, in short:

Concat function is faster than working with StringBuilder for the number of strings entering the function is known.

11
  • 1
    Thank god someone has written a good answer on this. Welcome to Stack Overflow, btw!
    – usr
    Sep 25, 2015 at 10:25
  • Thanks and... i'm not new ^^; Sep 25, 2015 at 10:27
  • But you've only recently started answering a significant amount of questions. I have noticed.
    – usr
    Sep 25, 2015 at 10:28
  • 1
    Actually I was looking for an answer which focuses on prepending a string via a StringBuilder or a string method itself. I'm creating the messageString via a StringBuilder which actually has a performance benefit. This messageString could finally be empty or a couple of dozen lines long. If the messageString is finally not empty an aditional string sould be prepended. Especially there my assumption was that using the StingBuilder would not be effective because the whole string inside the builder needs to be some kind of shifted. But I don't really know how it works internally. Sep 25, 2015 at 14:30
  • 1
    The string in the StringBuilder does not shift since the StringBuilder contains the 2 of your strings and their position until you use its ToString method, which mean prepending just add your string to the head of the list and waits. Sep 25, 2015 at 14:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.