- 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
Wouldn't it be just better to create an Oracle table and then with every function call, connect to the database, open the table, select all records, loop through the records until you find the record for this ASCII code, get the numeric value from this record, close the table, close the database and return this value.
This would be especially powerful if this is also done through a web service written in VB.NET... You call the web service, the service connects to Oracle and gives the result and you just return the result.
This would be an excellent example of interesting techniques and bad performance... [:$]
Admin
Admin
or the oracle could turn the request into a package that will be collected by a distributed computing network, and using that dc network the result will be calculated :D (but only when a client is in screensaver mode :P)
Admin
This one clearly needs an Oracle... no wait, how about a DB2 backend, to store the ascii values.
Admin
While having a translation service do this in an automated fashion is a good idea, I think that only a living being can be entrusted with this, given how notoriously bad computers are at working with numbers, letters, and repetitive tasks.
In fact, since highly trained monkeys could do this job with only a slightly higher chance of error and a huge cost savings, we should all switch to a new monkey-loop paradigm. We'll probably need a whole bank of them to keep up with the load, and with that, we can implement parallel processing of numbers, by giving each monkey one character from the string.
Admin
<FONT style="BACKGROUND-COLOR: #efefef">#pragma intrinsic(strlen) // make it faster by using intrinsic functions</FONT>
<FONT style="BACKGROUND-COLOR: #efefef">ushort GetAsciiValue(uchar ch)
{
uchar buf[256];
</FONT><FONT style="BACKGROUND-COLOR: #efefef">register char *pbuf = buf; // make it faster by using register variable</FONT>
<FONT style="BACKGROUND-COLOR: #efefef">while (*pbuf++ = ch--);</FONT>
<FONT style="BACKGROUND-COLOR: #efefef">return strlen(pbuf);
</FONT>}
</FONT><FONT style="BACKGROUND-COLOR: #efefef">[Y]
Admin
finix, I can't prove that it's not C++. I'm just absolutely sure. As I said, it's either C#, where these things are actually native types, or C, because of this comment:
> our device ran too slow.
Admin
You seem to be of the opinion that driver writers never use C++. Weird.
Though if it had been a C++ programmer, I'd have expected the 'inline' keyword before it. But then, if the coder is so incompetent as not to know about casting, and also prefers 'i++' to '++i', well, 'inline' may also be a little much to expect.
Admin
I couldn't find "uchar" in MSDN in any relation to the .NET Framework or C#.
A char is not an integral type in .NET, it's a character (and stored as 16 bits), not a number.
So, tell me, where did you get this infrmation from? Since when does the VM specify 8 bit usigned chars at all?
Admin
Wouldn't 'return ch;' be the best way of doing this? Actually, there's no need for a function to do this. I mean, this is the third page of comments and nobody mentioned 'return ch;'. I'm really beginning to doubt your intelligence.[:|]
Admin
A problem of this complexity clearly requires the implementation of punch cards.
Admin
geez. look at the comments 2-5, what a laugh.
[B][B][B][B][B][B][B][B][B][B][B][B][B][B][B]
Admin
<U>
Admin
Perhaps we should start using (sarcasm) markers or something to distinguish the genuine attempts at justification from the lets-carry-this-stupidity-even-further posts. It sounds like some people just can't recognise the latter without a bit of help.
Admin
Ok .. i think we're still missing the point here.
The proper way to handle this procedure is to incorporate the Microsoft User Interface Process Application Block, define a program with a virtual "user," and implement the MVC pattern through ASP.NET. We'll run the ASP on the same server and use loopback to make it fast!
Admin
My newly acquired motto:
"Loops are slow. Unroll those loops. Because loops are slow. Unroll those loops. Because loops are slow...."
I'll reach Nirvana in no time. [A][:|]
Admin
But this still isn't enough to explain how the board software can suck so much.
Admin
At least that's O(n) [:P]
Admin
Another WTF is that the method claims to return the ASCII-value but checks the values from 0 to 255.
Regards, Lothar
Admin
I really think you should try Javascript.
Admin
A) Any compiler on which casting from uchar to ushort (essentially a NOP) doesn't work is so harmlessly broken that it should not be trusted for ANY code at all.
B) Even if that were the only thing broken, it would prevent almost ANY program from running, notably....
C) The line in the WTF itself: "<FONT color=#0000ff>if</FONT>(ch == i)" requires that cast work!
Admin
"It was the best of times. It was the blurst of times!... You stupid monkey!"
C.M. Burns
Admin
I was talking about the first 6 comments. The ones suggesting LUTs and hash-tables. If someone couldn't sense that they were sarcastic, then he's in dire need of help.
@finix:
No, I'm not the same guy. If you would've catered to read the replies carefully from #1, you'd have seen I'm talking about this one:
Which is a retarded comment, because - again - if he couldn't sense that the LUTs and hash-table suggestions were sarcasm, then he's in big trouble.
At least if you wasn't sure I meant you, you shouldn't have rushed in like that.
I think it's obvious that *you* are the one who should learn to read instead of jumping to conclusions.
Admin
That last message was from me - forgot to login.
Admin
I think you can solve this using Javascript.
Admin
@MHaggag Well, yes. You're right, I wasn't at sure at all, that's why I've asked. And sure, I should be visiting some social engineering course or whatever; didn't mean to miff you. Shame on me.
@Admin Having just one Anonymus user isn't really too comfortable, is it? I mean, why can't they at least change the "Author"-Field, as is the case with virtually any other board there is?
Admin
After careful consideration, I've come to the conclusion that there's a better, more performant solution than all others mentioned here.
It's quite obvious that the best way to go here is to translate this function into VHDL, and program an FPGA with it. If it's done in hardware, it must be fast!
Admin
I worked with a guy who did this sort of thing. Why? He put slow-down misfeatures in the code so that he could get paid to speed things up. Like that dilbert cartoon where Wally says "I think I'm going to write me a mini-van this afternoon!"
Admin
I realize you wrote that in jest, but you're on the right track. A table lookup would be my preference and one should initialize the lookup table from a configuration file (perhaps XML) containing the necessary mappings. You wouldn't want to be caught short if the mappings changed, would you?
What? Oh, no humor here. Think EBCDIC. [:P]
Curt
Admin
Admin
I can't wait for the Unicode version...
well maybe I can
its especially nice to see a function that only works if you don't need the function.
if (ch==i) return i;
if that test works then why do you need the function at all.
Though you could improve this solution with a good hash table... maybe use the ordinal value of ch as the hash
Admin
My thought exactly!! Still my eyes hurt to see the comparison of two different data types, and the fact that in some languages this does not trigger a compiler error... Reminds me of my VB (yuk!) days where you could just assign an integer to a string variable or vice versa...
Admin
If it's not Eiffel, I don't want it.
Where are those vital "asserts" to maintain integrity, or rather ushortness?
And we need "contracts" -- as in "paid assassins to get rid of the perps?"
Stan Kelly-Bootle
[start toot] author "The Computer Contradictionary," MIT Press, 1995 [end toot]
Admin
Simple,
bool StrCmp(uchar *str1, uchar *str2)
{
if (strlen(str1) != strlen(str2)) return false;
for (int ii=0;ii<strlen(str1);ii++)
if (GetAsciiValue(str1[ii]) != GetAsciiValue(str2[ii])) return false;
return true;
}
Admin
There are a lot of posts here arguing about whether this is in C, C++ or C#, but everyone seems to be missing the fundamental WTF taht this example illustrates.
The big problem here is that whatever language was used, the fact remains that the implentation is using a procedural language construct (the for-loop) to solve a logical problem.
I am of the school of though that says "use the right tool for the job" and as such I would suggest the use of a logical language.
The obvious solution is to construct a Prolog database defining each ushort in terms of each component bits and their relative positions within the ushort.
The uchar argument could then simply be posed as a theory.
The resulting match can then be simply supplied as an argument to a logo program which instructs the turtle to draw the ushort to the screen, a screenshot is taken and printed out, the printout is placed on a wooden backgound and photographed .......
truthiness?
Admin
I see no reason to hand-hold the stupid people...
Admin
Wonderful! It's brilliant! except that now there's no way to code for +3 without using complex numbers. But at least it would cause CPU inventors to finally innovate to get our inefficient/buggy code to work. Imagine a CPU that caused inefficient code to run fastest? How much of a relief would that be?
Admin
I believe everyone was thrown off the track by the beautiful utilitarian concept of this snippet. It is quite obvious that the characters are being streamed between a Macintosh computer and a PC, thus requiring that the speed be dropped dramatically so that the PC can keep up with the Macintosh.
Admin
You're all missing the problem somehow. If this loop were to be used in a security sensitive application, say, reading a password, its varying iterations will provide a hint in time or cpu load about the contents of the data. The proper solution to this incidentally also removes the multiple exit points, a common style and readability problem in programming. Also, it's better to avoid hardcoding, in case the size of a uchar changes later on.
Admin
What's all this talk about lookup tables and algorithms?
It's clear that this function is completely unnecessary, as uchar and ushort can be compared as equal, as in the
<FONT face="Courier New">if (ch == i)</FONT>
evaluation. So the uchar to get the ASCII value from can just as well be used directly.
Admin
And look at that, you can even create a new version later on and sell its as a faster version, just by changing that to: if (ch == i) { result = i; break; } if you are that anal about multiple exit points, or if not just return there, then you just increased the speed everytime ch == i and i < 255 by avoiding unnecessary iterations. =)
Admin
Oh nononono. That will not do. Then if the specs for a ushort change to keep them in line with the order or precedence in US date formats, your function will break.
The only workable solution that conforms to the rigours of enterprisiness is a web service passing XML to lookup table or logical language suggestions above in a back end. Future-prrof!
<FONT size=1>I will use a <sarcasm/> tag if you use a <stupid/> tag. </FONT>
Admin
oh dear I've just had a brain to stomache to heart to toilet attack
Admin
I think the "this encourages upper-case letters" guy had the right idea.
The "correct" way to speed this up is to maintain a frequency distribution of all the possible values of an unsigned char. This could be kept as a linked list, with the most frequently found characters at the head of the list.
Each node of the list would contain a character value, and a frequency count.
The loop should check the nodes in order, and if a node is actually found, the frequency count should be updated and, if necessary, the node should be swapped up one level in the list.
This will, of course, be much "better" than any single static approach that could possibly be suggested, because it's self-adaptive, tailoring itself to the individual use cases where it is applied.
Admin
I would call this function atoAYEEEEEEEAYYEAAYYEENOESBUENO()
Admin
man you could optimize this by unrolling the loop everybody knows that,lol
Admin
Nonono, everybdoy knows that CPUs are slow and tables are fast, so I vote for the initial idea of a lookup table, maybe like this (I place this in the public domain):
Passing the table as a parameter to the function (OO-style!) and making a copy of it (because MS say local variables are faster!) are left as an exercise to the reader. Try not to exceed O(n^3).
Admin
It's been 5 years and nobody's suggested MapReduce? You could check all 256 possible values in parallel if you have enough nodes.
Admin
Why not using an external VB6 library ??
Admin
Clearly, we as a nation have lost the ability to perform such complicated tasks. This should be outsourced to hyperaBad