tags:

views:

154

answers:

4

Is it from a sight of resources reasonable to use a StringBuilder already to concat two strings or is there a minimum concatenation operations that makes the StringBuilder efficient?

+6  A: 

String.Join is the fastest one as it allocates all the required memory in one operation. See this article: StringBuilder vs. String / Fast String Operations with .NET 2.0

Giorgi
For only two strings, `+` is more readable and just as efficient.
Brian
+6  A: 

Concatenating two strings with StringBuilder won't give you any benefits, since the result still has to be converted to a string - so concatenating them directly is one allocation + two copies. StringBuilder can't do any better - only clutter your code.

EFraim
+6  A: 

...the String class is preferable for a concatenation operation if a fixed number of String objects are concatenated

Yuriy Faktorovich
This answer really deserves more upvotes, for in practice, this is exactly the advice to follow: StringBuilder in loops, String class concatenation methods for fixed number of strings.
Stephen Swensen
Indeed, it should be actually the accepted answer. Sorry, I was yesterday a little intimidated and accepted then to fast.
Heinz K
@Heinz: you should be able to change the accepted answer if you want.
Joren
@Joren: Yes, I have seen this, but I don't find it fair to Giorgi because I already accepted his answer.
Heinz K
@Heinz: I think accepting a sub-par answer is unfair to anyone else who later comes to this site looking for answers.
Brian
@Joren: Your right. I will change.
Heinz K
@Heinz: That wasn't me. :)
Joren
+7  A: 

If you're asking the question, you're probably making a decision on what technique to use in order to get good performance.

If you're attempting to get good performance then you probably have a suite of carefully-designed performance tests which clearly show the differences between two different techniques, measured along a set of realistic, user-focussed performance scenarios.

If you have such a suite, why are you asking us? Just run it both ways and see which one gives you better performance!

If you don't have such a suite, you're never going to get good performance out of a slow application by asking questions random people on the internet who do not know your customers, your code, or your scenarios. Build yourself a carefully-designed suite of performance tests which allows you to measure the difference between two techniques and compare that against a realistic set of user-focused performance goals.

Eric Lippert
First of all, its curiosity. Neither I have the need of a carefully-designed suite of performance tests, for that my projects are really to small nor do I have a problem that I must optimize my programs in such a low level area as concatenating of two strings is. But many people talk about using StringBuilder and this made me curios if there is a minimum amount where the breakeven is. There exist also questions with no big importancy but if there is a platform to ask, wyh not ask? And if its below your level, ignore the question.
Heinz K
Sorry Eric, had to down vote you here: your answer seemed kind of rude and was definitely unhelpful. Surprising coming from a senior developer on the C# compiler team (a product for which I have tons of respect)!
Stephen Swensen
Stephen, what's so rude about Eric's answer? IMO, his answer is bang on. If you don't have performance problem, don't worry about StringBuilder, worry about writing readable code which solves your business problem. If you have a performance issue, you MUST profile the code first and then work on the hot spots identified by the profiler. There is simply no other way.
SolutionYogi
While I think this answer is pretty snarky, I do absolutely agree with the assertion that this is so simple to test and requires so little code, why ask instead of performing the test yourself?
Marc
@Stephen: Indeed, people often attribute completely imaginary rudeness when interacting via a low-fidelity, emotionally impoverished, and time-delayed medium like this one. It is very easy to attribute brusqueness to what is actually brevity, or condescention for what is actually mere accuracy. This is an extremely well-studied phenomenon; I'm sure that if this subject interests you, you can find plenty of papers on it. You might also be interested in my essay on the subject, which is here: http://blogs.msdn.com/b/ericlippert/archive/2008/02/20/how-to-not-get-a-question-answered.aspx
Eric Lippert
I excuse me to ask such a simple question without digging before in the topic. I searched for a simple answer to a question that I had but that was not very important to me, something that would be “nice to know”. This was a mistake. I didn’t want to waste the time of anybody. Sorry, I misunderstood the nature of stack overflow.
Heinz K
@Eric I feel like your goal in answering questions on SO is for the OP (and anyone else reading his answers) to become better programmers, with providing an answer being the vehicle to do so. That said, in this case I think you should have copy/pasted his other performance answer for this type of question (i.e. "write your code to be readable first, then worry about efficiency") rather than this answer. Though as a more general piece of advice, I prefer Yuriy's advice, which is more relevant to the question. Doing things "right" makes code more readable to experienced programmers.
Brian
@Brian: I note that it is rather presumptuous to claim you know what my goals are and how I'm attempting to achieve them. I do not claim to know what your goals are; however I note that if you would have answered the question differently and better then **you could answer the question differently**, rather than analyzing my technique in light of my presumed motives. No one is stopping you!
Eric Lippert
@Eric: I apologize.
Brian