- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Performance arguing people, lend me your ears er... eyes.
The performance difference between StringBuilder and simple concatenation is minimal when you're talking about a couple of hundreds.
It's start getting interesting when you using loops, and loops in loops. Generally af you'r looping to add stuff, use a StringBuilder. With the minimal performance difference it's way nicer and more scalable.
The biggest difference between StringBuilder and concatenation is memory usage. Concatenation will copy a string with every addition, hogging up memory until GC. StringBuilder will not.
That is way StringBuilder is considered the better practice when combining more than [insert number] strings.
Admin
And concatenation in one statement will allocate memory just once anyway. What is more it will join string literals together which will result in even better performance.
Admin
Of course not. The NDA falls under the NDA.
Admin
Admin
With a couple of assumptions, such as not looping, or not walking down some kinda of object structure, I find (in .net) String.Format(str, object[], IFromatProvider) a good way to handle string concats
Under the covers, it creates a StringBuilder and uses AppendFormat.
It's a win win, looks nicer than string concacts, and easier to use and more elegant than StringBuilder.
Admin
Whoa there sonny. You're assuming two things: a.) The library code writers are actually good at writing accurate documentation. From some of the WTFs I've seen in MSDN documentation I'd be wary. b.) The library code writers actually wrote the documentation and not some tester or tech writer.
Admin
Indeed, the only thing that Java and C# get wrong is that you have to know about using these classes at all; many other language implementations do this, but hide the details from users of the language entirely.
Admin
I might also suggest that the code and database would be more maintainable if the INSERT statements listed the column names, rather than just assuming a given column order. This would make adding new columns easier -- a problem the original author pointed out.
(But this is really a trivial point next to the unreadable generated code that abuses StringBuffer/StringBuilder instances!)Admin
StringBuffer is java. But there is absolutely no possibility the code given above is faster than return customer.FirstName + " " + customer.LastName + "\r\n". It is conceivable that code with four append operations and no + operators could be faster, but that's not what's given above.
Admin
Repeatedly taking the .ToString() of it would also be a drain on performance, if his program were foolishly doing so.
Admin
mm..actually for java there's not that much garbage collection going on with strings. java utilizes a string cache that drastically reduces garbage collection on string type objects. ever wondered why String a = "abc" String b = "abc" (a==b) //usually return true. it's very weird that this works, since the check here compares the reference. it is equal because usually you literally get the same instance.
this is not the case for the string builder tho, so the string builders will get gced.
i do wonder about the line "memory usage will grow more or less exponentially with the number of concatenations" while it is true that StringBuilder will allocate twice the previously allocated memory whenever it runs out of space on .Append , it still grows linearly with the number of concatenations.
Admin
Admin
You get similar things in .net, where two variables assigned 'ABC' with string interning switched on will result in true when evaluating there references.
The string's equality methods are overloaded (and sealed), so that it does do a value comparison on string.
hence,
string a = "ABC"; string b = SomeUserInputControl.Text // where the user enters "ABC"
a == b will evaluate to true.
Admin
Admin
How does string builder do all the concats faster? I understand that it will use less memory, but why would it go faster?
Admin
With the concat, each concat copies both strings into the newly created target string. So the string under construction gets copied each time there's a new string tacked onto it.
The StringBuffer only copies the string under construction when a buffer reallocation is needed; this happens log n times, where n is the number of characters, or 0 times when the size is set big enough beforehand. The strings being tacked on to the end, of course, must be copied in both situations.
Admin
Now I see why you work there...Coz' your head is filled with it - Air and space and a lot of BS)
Admin
I seriously hope you work in the billing dept. If you are in engineering, well, at least now we know why planes crash...
Admin
Well, I would hope so to, I would hope that there is no .net code in anything that flies through the air. .net implies windows (ok, ok mono.net, but seriosuly?), windows is preemtive and i'd hate to think of the consequence of a process adjusting the flaps, suddenly being switched.
and err, windows implies BSOD
though could be worse, could be Java, doubt the plain would ever take off under the weight of bloated code </troll>
Addendum (2009-01-30 11:23): Plain?! I mean Plane... oops
Admin
Clearly this was generated by a recursive method. And any programmer worth his 2 cents can tell you that recursion is the most powerful and elegant programming technique in the whole universe and should, no, must be used any time it is possible to do so, provided the programmer can master is subtle complexities. This is not a WTF, this is genius at hand.
Admin
Looks like LISP to me..
Admin
I just have to post that I had an excellent chuckle at the "Anonymous, Aerospace, NDA, I-can-tell-you-but-then-I'd-have-to-kill-you incompetent" poster for a quite magnificent troll, well done!
:)
Admin
I've seen some crazy sh** in my time... this takes at least a small portion of the cake.