- Feature Articles
- CodeSOD
-
Error'd
- Most Recent Articles
- Stop Poking Me!
- Operation Erred Successfully
- A Dark Turn
- Nothing Doing
- Home By Another Way
- Coast Star
- Forsooth
- Epic
- 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
Admin
Why doesn't Dan D write some code-rewriting code? Does his manager now fetch the garlic and the stake when someone mentions code generation?
Admin
I'm going to guess your code looks something like this:
string test = "oh " + "my " + "god " + ", " + "this " + "is " + "faster";
StringBuilder sb = new StringBuilder(1); sb.Append("oh "); sb.Append("my "; sb.Append("god "); sb.Append(", "); sb.Append("this "); sb.Append("is "); sb.Append("slower "); sb.ToString();
Admin
Admin
At least Aaron backed up his claims.
Admin
Admin
I too am wondering (1) how come they can't just rerun the code generation tool when the database changes and (2) why they can't fix the code generation tool and then rerun it.
Admin
He admitted that the first result looked wrong and tested it again. Then he posted both results. That's called integrity. Most people would have ignored the first result. That said, he should run the test a large number of times (maybe 100) and then post the averages to reduce the effects of one off quirks like jitting delays or some other unexpected interruption. You have still posted no results and no code and can't show anything to back up your claims despite two links and one post all showing the opposite. So I'd say if most cases (except Mr. Anonymous super special aeronautic apps), StringBuilder is demonstrably faster for all but the smallest string concatenations.
Everybody note: if you are working in aeronautics, make sure you use string concatenation. Aeronautics is special and the regular laws of physics don't apply.
Admin
Admin
Now thats funny
Admin
Agreed. Items 2 & 3 means that this wouldn't compile in either language. I'm calling BS.
Admin
Admin
Admin
Yes, but if all of your work is covered by the NDA, haven't you violated it by posting the results?
And by your own words, this lesson would hardly qualify as "very important". Apparently, the difference between the two is so negligible that unless you're writing an app to do nothing but concat strings all day, it doesn't matter what you use.
Even considering that logic, the concat operator is fugly. In a sea of well written, elegant code...a concat operator stands out like a rear view mirror duct-taped to a Ferrari.
Admin
TRWTF is coders having an extended argument over use of StringBuilder.
Admin
At my current job, there was a guy who wrote code that looks a lot like the original WTF. Except he did it by hand. And since we're in VB6, he had to write his own StringBuilder class first.
Admin
I wish books about basic programming did not mention StringBuffer / StringBuilder at all. SB only makes sense in loops and recursiion and such. Programmers who don't understand strings are less likely to do harm if they don't know about SB. What is more some book authors don't understand how string concatenation works. I've seen books that advise using SB if you have more than 3 + concatenations. WTF?!
Admin
I'm more than happy to post the "performance testing" code - I am certainly not bound by any NDA. But I have a strong feeling that Mr. Anonymous is just trolling; the giveaway was his comment about other tests not "exercising the relevant parts".
Admin
Sorry, but no amount of BS is going to get you off the hook.
An NDA for a StringBuilder vs. concatenation benchmark? Who are you trying to kid?
Why should anyone to believe you when you
post using "Anonymous" so people can't differentiate you from every other troll
post nonsense drivel about benchmarks you can't back up
claim you can't post your benchmark code because of NDAs (which is utterly ridiculous)
have a benchmark (that you can't produce, of course) that totally disagrees with the makers of the library's own benchmarks (which they provide code to justify)
Come back when you actually have something to say. Right now, you're just spewing senseless, useless noise.
Admin
Admin
This does seem to be a mix of java and c#. Maybe the code is compiled with IKVM which allows java code to use the c# library (and thus .toString() would be an alias to .ToString())
Admin
I expect them to employ people who actually know what they're talking about, and not people like you who blow smoke.
Who do you really work for?
Admin
Clearly, that NDA must also include these asinine comments?
"[Anonymous] then goes on to prove that black is white and gets himself killed at the next zebra crossing."
And there was much rejoicing.
Admin
sigh
Alright. I'll play your silly little game: TRWTF™ is jQuery.
Admin
Cool! That code doubles as ASCII art. Proof that programming can be an art.
Admin
The 2Gb RAM you bought to get the latest Windows to run is already invested. So, why not use StringBuilder and eat up that RAM?
Admin
I think you'll find that since .NET (and Java) is garbage collected, you'll eat up less memory using StringBuilder (especially if you set the capacity to something sensible). If you look at the graphs in the link you'll see that the garbage collector is invoked much less often with StringBuilder than with string concatenation because all those copies of strings are stuck taking up memory and waiting for collection.
Admin
static long NUM_LOOPS = 1000000; static long NUM_APPENDS = 100; static String APPEND_STRING = "something";
StringBuilder = 4.28s, String = 32.8s
Changing NUM_APPENDS to 10: StringBuilder = .61s String = .8s
Changing it again to 5: StringBuilder = .33s String = .27s
Your attitude is exactly why StringBuilders are... oh nevermind, it's just easier to call you stupid.
Admin
TopCod3r at least was sometimes funny...
np: Near The Parenthesis - Nidae (Intelligent Toys 4)-
Admin
I've had the pleasure to see a system explode for real - like in the movies, with fire and smoke et al. It was on an oil rig where I was installing a monitoring system for mooring lines, and I had received serious management grief for insisting on optical isolators in our 4 PC unit (they cost money, and it was deemed over the top).
The guys who were supposed to supply us with wind data also handled generator power, and just finished rigging. Their PC based system powered up, hard disks started spinning, OS loaded, the core apps started, preparing for data acquisition. As soon as the relays on the data acquisition card clicked in, the thing literally exploded internally - we're talking about a system case that ended up being rounder than designed, and I actually never expected that possible. I presume it must have been capacitors that provided the bang.
We heard later that a wiring mistake had been responsible. The DA was supposed to have been fed off a normal 20mA loop, but was instead patched straight into some 480V power line, hence the enthusiasm when the relays allowed this into unprotected circuitry.
Complaints about optical isolators disappeared :-).
Admin
In fact, the memory characteristics of concatenation vs. StringBuilder are the same as the running time characteristics; memory usage will grow more or less exponentially with the number of concatenations.
(I say "more or less" because eventually the garbage collector will kick in and free all of that memory, except for the one result string that's still assigned, which takes up exactly as much memory as the StringBuilder would be taking up.)
Admin
oh sweet jesus, i hope you just forgot to wrap that statement in sarcasm tags. you may have not looked close enough so im going to point it out to you, THATS NOT ACTUALY USING A STRINGBUILDER ITS DOING string str = customer.FirstName + " " + customer.LastName + "\r\n"; sb.Append(str);
sorry for yelling but you shouldn't say things like that, its scary.
Admin
Actually this would be simple to maintain. The first time it needs to be modified you simply throw this crap away and write something better in its place. I'll never understand why people will jump through hoops to avoid fixing crap.... Surely overloading columns is more difficult and harder to maintain than just replacing this BS in the first place....
Admin
Hmmm, I wonder if this code was inspired by a book on optimizing Java? I read one in the 1.4 days, which pointed out how the compiler handles string concatenation by creating a string buffer, appending the first side and then the second side, then converting it back to a string, and would do this for each pair, even in a multiple concatenation. So the author said to use StringBuffer yourself and don't convert toString until afterwards, and showed timing loops to support the claim.
Of course this example seems to be the exact opposite of that advice, so maybe he read the book backwards...
Admin
Admin
C# version that compiles and runs:
Admin
Can I see this NDA please? I have my agreement-signing pen here ready to go. (It's blue ink, I'm assuming that's okay?)
Admin
"Extra effort" should only be required for doing things wrong.
Admin
He combined the memory waste of string concatenations with the uglyness of StringBuffer! It's things like this that tell you you're dealing with a genious instead of a mere mortal...
Admin
I don't do Java or C#. What does StringBuilder/Buffer do to increase concatenation speed? Keep references to raw data values and (maybe lazily) print them when needed? That would certainly be faster than copying arrays of chars...
Admin
In C# and java Strings are immutable. So to do A + B; A new string is created, contents of A are copied, contents of B are copied. Fairly slow. Each time you need to append a string, BOTH strings are copied. But a StringBuilder is a mutable string. Basically a big buffer is created. As long as their is room in the buffer, the new string is just attached onto the end.
Admin
post = "
"; post.append("I'm not sure that I entirely agree.");Admin
And how long have you been working with this? Theres a WTF right there
Admin
Mental note: avoid work in aerospace industry.
Admin
not in java you haven't
Admin
Can I suggest that in the time you spent telling the world your problems you could have tweaked the code generator and fixed the problem. Another example of a developer who complains but never fixes.
Admin
To me, the real WTF is this:
The original solution obviously stinks. Now add in bit-shifting and string manipulation...things can only get worser and worser from here.Admin
Somebody explain to me again why data-access code is boring and repetitive?
Admin
The code in the article looks to .NET (upper case T on ToString).
Admin