- 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
The first one (copy char-by-char). That has to be faster than several nested replace calls. How do you think replace works anyway? Just because it's a built-in function, doesn't make it magic.
Admin
Admin
Admin
The real WTF is the comments.
Admin
That seems very unlikely. In the best case the string has no spaces at all, so the replace method would not have to do any replaces and would be able to skip as much as possible. The most it can skip is the length of the string its searching for, so if it's looking for a ten space string, it only needs to look at every tenth character... and in general for an X length string, it will iterate over at least 1/Xth of the characters. So the number of characters the replace method will look at is 1/10 + 1/10 + 1/3 + 1/2 + 1/2, or 153.333% as many as the length of the original string. Meaning that a single pass method should easily beat it (unless it manages to be extremely less efficient on the replaces).
Admin
I don't see the problem guys.
Admin
Admin
Here's my WTF python solution:
Admin
Oooh, even better.... one line:
Admin
In theory running over the string once rather than the 5 times that the original code snippet does would be more efficient, however (it's been a while since I've done VB6) isn't VB6 runtime interpreted? And are the builtin functions interpreted or written in C in some DLL? If that's the case you'd have to run tests on the 2 to find out.
But if the original dev had really gone to all that trouble they should have left a comment explaining why they chose the method that they did...
Admin
Why is no one thinking "enterprisey" here?
SELECT GoodValue FROM FixBadValuesTable WHERE BadValue = '%1' OR GoodValue = '%1';
Admin
Admin
they forgot the case
if( string == null ) return fileNotFound;
Admin
In addition to the inefficiency mentioned we have yet to account for the additional array copies that are being made. The best case would be to make one array copy after finding each replace location. So for each replace you have an additional full iteration after you perform some efficient iteration to find the replaceable locations. In other words even if you do a single replace with the 10 length you will iterate over 110% of the length.
Admin
I'm pretty sure VB6 compiled to native code, but I might be wrong and you make a fair point. You'd have to try it to be absolutely sure, but I'm not going to pollute my machine by installing VB6 on it. My general feeling is that a single pass method (iterating over all the characters of the string) has to be faster than multiple calls to replace (assuming the single pass method is sanely written, of course) because replace can't take advantage of the fact that it's being called nested. So why it could be faster (as per your point), I'd be very surprised if it turned out that it was.
Admin
Admin
Calling trim() reduces the lines of code but forces a string copy which may be inefficient.
Looping through the elements might be the better solution: I don't know Java well enough though.
Admin
Admin
Admin
Brillant!
Admin
The "fix" was sarcastic, c'mon now.
Admin
I think the comments are funnier than the article. You're fighting over the accuracy of a parody. It runs through the string and sets blank to false every time it hits a character that isn't a space.
p.s. use Trim()==""
As for the second one, that's nothing Replace(/\s+/," ") won't fix.
Admin
Remember folks, as we learnt yesterday, people who write code this bad aren't just bad programmers, they're probably paedophiles too.
Admin
First of all, the comparison needs to be made with -1, not 0. Also, what about special characters, such as '&' and 'ˬ'?
Admin
Admin
In fairness, I used to work for a company that had a rule that all reports must end with the line "*** END OF REPORT ***". Their reasoning was that this made it easy for someone to verify that he indeed had the entire report and that the last one or more pages had not failed to print or been lost or whatever.
Admin
The RemoveCharacters function uses 5 calls to Replace, the sequence replacing 10, 10, 3, 2, 2 spaces with 1.
Keeping the 5 calls to Replace, the performance of this function can be improved by using the sequence 22, 7, 4, 3, 2.
This new sequence will handle all cases up to 460 contiguous spaces, compared to the original 208.
The largest number of contiguous spaces that can be handled is 3696, vs. 1200 for the original, and the total number of cases it handles is 2078, vs. the original 704.
Can anybody else do better using 5 calls to Replace?
Admin
Yeah, some of his other advice was similarly founded. It was from another student in this class that I first heard the truism "Those who can't do, teach." And it must have circulated widely enough that it got back around to him, because he felt it necessary at the beginning of one class to spend ten minutes telling us about his illustrious pre-academia career and his decision to take a huge pay cut out of a selfless commitment to public service. Ummm hmmm.
Admin
Admin
Surely reading code is exactly like reading books, amirite?
Admin
Sometimes, high-level OOP languages over-complicate things. We don't need a String class to test for a blank string.
Here is a (nearly-right) 6502 Assembler implementation. It reads a char-string at address #2000 with 20 characters. It starts at the last character and counts down to the first. Each trip through the loop it compares the current character to a SPACE (ASCII 32).
; INIT LOOP INDEXES 4000 LDA #32; SPACE 4004 LDX #20; LENGTH OF CHAR-STRING ; START LOOP 4008 CMP 2000, X; CHAR-STR AT ADDR 2000 INDEXED WITH X 4012 BEQ 4024 4016 DEX 4020 BNE 4008 4024 ; LOOP TERMINAL ADDR
Admin
Admin
Might work, but what if the strings are more than 256 bytes? EBCDIC?? Some things DO have limitations. We should understand ALL of them.
Admin
You and those replying to you, as far as I've seen, have failed to see that in fact this only works up to 209.
You can run this if you feel like that:
If I had some spare time I'd try to find out the properties that the sequence of numbers has to meet to get good results up to the maximum possible number with the shortest length. The most obvious correct sequence is {A(n+1) = A(n)*2-1; A(1) = 2} but [2,2,3] gets better results for that length (9 vs 11).
BTW: VB really had to be a crappy language not to have RE nor loops, not even Turin complete in that case, if I'm not mistaken. I don't know how the replace function works without loops but using them it's fairly easy. I tried to make it efficient, anyone interested can post another solution and compare.
Admin
Admin
Admin
Admin
ZINGER!!!
Admin
Admin
See my comments above. I give a sequence that works better, 22, 7, 4, 3, 2.
Admin
Admin
FTFY
Admin
Admin
Admin
Admin
A fabulous trolling, or else citation needed.
Admin
TRWTF is that none of these handles the example of a true whitespace character, which is sometimes "\n", "\t", "w". The bigger WTF is that this is the third time I've pointed it out, but the idiot mods keep deleting it.
Admin
Smith, Preston. "The Shroud of Turin." Encyclopedia Britannica. 2009
Admin
Peer-reviewed primary sources?
I don't have access to Britannica, but, for what it's worth, Wikipedia's article says nothing of the sort.
Admin
Interesting logic...