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,983 of 33,346    |
|    Richard Damon to James K. Lowden    |
|    Re: justifiable, ancient, or silly?    |
|    03 Mar 12 12:10:11    |
   
   From: news.x.richarddamon@xoxy.net   
      
   On 2/29/12 6:54 AM, James K. Lowden wrote:   
   > 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 remember unrolling loops 20 years ago; even then I thought that was   
   > the compiler's job. These days size is all because cache is king.   
   >   
   > --jkl   
   >   
   >   
   My guess, is that IF this function was used enough to be speed critical,   
   and there were enough instances with common sub-strings inside strings   
   of equal length, that this method would allow extracting the testing of   
   the common strings, and doing that test once, and not need to repeat it   
   for each of the strings.   
      
   It is also possible that on some systems, writing it this way moved the   
   characters into code space instead of data space, and perhaps is better   
   protected from being trashed.   
      
   Some processors may also be able to execute this code particularly   
   efficiently. A big question that comes up here is are the current   
   conditions that this code executes under enough different that this form   
   is no longer optimal, and then, is there enough benefit to changing it   
   to pay for the cost to change it.   
      
      
      
   --   
    [ 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