• (cs)

    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... [:$]

  • (unregistered) in reply to Jeff S
    Jeff S:
    [image] spooky action at a distance wrote:

    [image]  wrote:
    You could have sped up GetAsciiValue() by making it a remote procedure call to a dedicated server maybe. ;)
    Better yet, turn it into a .NET Web Service!

    That way you could write it in (wait for it) JavaScript!

     

     
    Javascript to the rescue again! and, of course, we can throw some XML into the solution this way as well!  Technical perfection!  And you can tell your boss that with all of the technologies you are using, it will be "robust and expandable" and will require 6-8 months of coding + testing at $150 /hour! Big Smile
  • (cs)

    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)

  • (unregistered)

    This one clearly needs an Oracle... no wait, how about a DB2 backend, to store the ascii values.

  • (cs)

    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.

  • (unregistered)

    <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 style="BACKGROUND-COLOR: #efefef">[Y]

    </FONT>
  • (unregistered)

    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.

  • (unregistered) in reply to

    :
    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.

    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.

  • (unregistered) in reply to CornedBee

    CornedBee:
    Looking at the code only, it's most likely C#, where ushort and uchar are keywords.

    I couldn't find "uchar" in MSDN in any relation to the .NET Framework or C#.

    CornedBee:
    1) Neither C nor C# requires even an explicit cast for this. The cast from integral type A to integral type B where the definition range of A is completely embedded in B's definition range is always implicit.

    A char is not an integral type in .NET, it's a character (and stored as 16 bits), not a number.

    CornedBee:
    2) If this code is, as I suspect, C#, then uchar will be 8 bits regardless of the machine it runs on - the VM specification requires this.

    So, tell me, where did you get this infrmation from? Since when does the VM specify 8 bit usigned chars at all?

  • (unregistered)

    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.[:|]

  • (cs)

    A problem of this complexity clearly requires the implementation of punch cards.

  • (cs) in reply to KraGiE

    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]

  • (cs)

    <U>

    <FONT color=#9acd32>geez, just noticed pages 2 and 3, didn't see that!!!!! it seems the worst coders come here too. maybe you should sell home-study courses from this 
    site, ud be billionaires!!!</FONT>

  • (unregistered)

    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.

  • (unregistered)

    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!

  • (cs)

    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][:|]

  • (unregistered)

    But this still isn't enough to explain how the board software can suck so much.

  • (unregistered)

    At least that's O(n) [:P]

  • (unregistered)

    Another WTF is that the method claims to return the ASCII-value but checks the values from 0 to 255.


    Regards, Lothar

  • (unregistered)

    I really think you should try Javascript.

  • (cs) in reply to

    :
    Obviously casting from uchar to ushort does not work on this system, or the programmer would have used it. Maybe it's a compiler bug or something.

    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!

  • (cs) in reply to foxyshadis

    foxyshadis:
    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.

    "It was the best of times.  It was the blurst of times!... You stupid monkey!"

    C.M. Burns

  • (unregistered)
    unfortunately, it appears you have not been browsing this site for too long ... it is nearly impossible to distinguish between some of the real and sarcastic comments -- there have been MANY serious WTF comments.  In this thread alone, there's 3 or 4.

    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:
    Oh yes? You're the guy I quoted from, are you? (I know I should *sense* whether you are that *someone*; but, then again, you already pointed out that I'm not as witty as you. So sorry.)

    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:
    right, this is retarded. the code is just taking a char and returning the same value as a short.

    The comments seem like just as much a WTF as the WTF.

    I think I'd have to use Jython to make it work right, or maybe a micture of VB and C#

    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.

    Anyways, if you mean those "improvements" of this method posted earlier - even I caught their drift, imagine, especially liked the function-array - you should learn to read, then come back to see what I've quoted & commented on.

    I think it's obvious that *you* are the one who should learn to read instead of jumping to conclusions.
  • (cs) in reply to

    That last message was from me - forgot to login.

  • (unregistered)

    I think you can solve this using Javascript.

  • (cs) in reply to

    @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?

  • (cs) in reply to finix

    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!

  • (unregistered)

    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!"

  • (unregistered) in reply to Jeff S
    Jeff S:
    I see I have not been let down by the last 3 comments so far ... keep them coming... any other good ideas on how to solve this difficult programming dilemma?  XML maybe?   


    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

  • (unregistered)
    You guys are awesome!  This is so funny.  Man this is funny.  I mean I was really laughing out loud at some of this stuff.  Freaking awesome!
  • (unregistered)

    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

  • (unregistered) in reply to
    :
    Is'nt there a ordinal function that will convert an ascii character to a number???


    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... 
  • (cs) in reply to Jeff S

    Jeff S:
    I see I have not been let down by the last 3 comments so far ... keep them coming... any other good ideas on how to solve this difficult programming dilemma?  XML maybe?   

    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]

     

  • Euncheese (unregistered) in reply to Menzel

    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;

    }

     

  • Some Idiot (unregistered)

    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?

  • (cs) in reply to
    Anonymous:
    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.

    I see no reason to hand-hold the stupid people...

  • Anony mouse (unregistered) in reply to

    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?

  • (cs)

    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.

  • Yann Vernier (unregistered)

    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.

    #include <limits.h>

    <font color="#0000ff">ushort</font> GetAsciiValue (<font color="#0000ff">uchar</font> ch)
    {
    <font color="#0000ff">ushort</font> i, result=0;
    <font color="#0000ff">for</font> (i=0; i<=UCHAR_MAX; i++)
    {
    <font color="#0000ff">if</font>(ch == i)
    result = i;
    }
    <font color="#0000ff">return </font>result;
    }
  • Roy (unregistered)

    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.

  • (cs) in reply to Yann Vernier
    Anonymous:
    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.

    #include <limits.h>

    <FONT color=#0000ff>ushort</FONT> GetAsciiValue (<FONT color=#0000ff>uchar</FONT> ch)
    {
    <FONT color=#0000ff>ushort</FONT> i, result=0;
    <FONT color=#0000ff>for</FONT> (i=0; i<=UCHAR_MAX; i++)
    {
    <FONT color=#0000ff>if</FONT>(ch == i)
    result = i;
    }
    <FONT color=#0000ff>return </FONT>result;
    }

    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. =)

  • (cs) in reply to Roy
    Anonymous:

    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.

    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>

     

  • kap (unregistered) in reply to Katja

    oh dear I've just had a brain to stomache to heart to toilet attack

  • (cs) in reply to kap

    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.

  • GrouchyAdmin (unregistered)

    I would call this function atoAYEEEEEEEAYYEAAYYEENOESBUENO()

  • cros (unregistered)

    man you could optimize this by unrolling the loop everybody knows that,lol

  • Anon (unregistered) in reply to cros
    cros:
    man you could optimize this by unrolling the loop everybody knows that,lol

    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):

    ushort GetAsciiValue (uchar ch)
    {
    
        char lt[256];
        ushort i;
        ushort j;
    
        for (i=0; i<=255; i++)
        {
            lt[i] = 0;
        }
    
        for (i=0; i<=255; i++)
        {
            for (j=0; j<=255; j++)
            {
                if(i == j)
                {
                    lt[i] = j;
                }
            }
        }
    
        for (i=0; i<=255; i++)
        {
            ch = lt[i];
            for (j=0; j<=255; j++)
            {
                if(lt[i] == j)
                {
                    return ch;
                }
            }
            return 0;
        }
    }
    

    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).

  • Adam (unregistered)

    It's been 5 years and nobody's suggested MapReduce? You could check all 256 possible values in parallel if you have enough nodes.

  • Kryptus (unregistered)

    Why not using an external VB6 library ??

  • DoOutSource (unregistered)

    Clearly, we as a nation have lost the ability to perform such complicated tasks. This should be outsourced to hyperaBad

Leave a comment on “For-Loop Illogic”

Log In or post as a guest

Replying to comment #29401:

« Return to Article