Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,963 of 33,346    |
|    Thomas Richter to All    |
|    Re: justifiable, ancient, or silly?    |
|    02 Mar 12 02:34:10    |
   
   From: thor@math.tu-berlin.de   
      
   Am 29.02.2012 12:54, schrieb James K. Lowden:   
   > I recently came across code testing passed strings that I'm guessing is   
   > intended to be fast, but to me looks just awful. Before I   
   > suggest/offer fixes, I want to make sure I'm not missing something   
   > obvious.   
   >   
   > The function is passed a length and a char*, and does:   
   >   
   > switch(len) {   
   > case 5:   
   > if( s[0] == 'H'   
   > && s[1] == 'E'   
   > && s[2] == 'L'   
   > && s[3] == 'L'   
   > && s[4] == 'O' )   
   > /* stuff*/   
   > break;   
   > case 8:   
   > if( s[0] == 'R'   
   > && /* etc. */   
   > }   
   >   
   > I can think of three ways in a minute to write that more concisely and   
   > idiomatically. The question is, is there now or was there ever   
   > justification for such elaborate hard-coding?   
      
   I would say it pretty much depends on the compiler. Depending on its   
   quality, I would expect a strcmp or memcmp to be inlined and to generate   
   code that is equivalent to the above. Even pretty old C compilers (not   
   C++ necessarily) I'm aware of did that. But then again, if this is a   
   non-mainline system (embedded, probably) with a poor compiler (vendor   
   provided, probably) and the function in question is really critically,   
   it *might* make some sense.   
      
   IOW, in almost all cases I agree with you; but then, after all,   
   profiling should reveal whether this micro-optimization makes sense for   
   your particular application. I would believe that it does not in most cases.   
      
   Greetings,   
    Thomas   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca