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 32,368 of 33,346    |
|    Ike Naar to Martin B.    |
|    Re: Techniques to avoid minimal scope in    |
|    05 Jun 12 17:32:04    |
   
   From: ike@sverige.freeshell.org   
      
   On 2012-06-05, Martin B. <0xCDCDCDCD@gmx.at> wrote:   
   > This is all nice and well, and there's certainly nothing wrong with   
   > this, until you move from primitive types to objects. (for a *certain   
   > kind of interface*)   
   >   
   > Example:   
   >   
   > // [C]   
   > void fs() {   
   > ...   
   > for (int i=0; i!=n; ++i) {   
   > string s;   
   > get_text(i, s); // void get_text(int, string&);   
   > to_lower(s);   
   > set_lower_text(i, s);   
   > }   
   > ...   
   > }   
   >   
   > Here, the string s will be destructed, it's memory released every loop   
   > cycle and then every cycle the `get_text` function will have to newly   
   > allocate the memory for the s buffer.   
   >   
   > It would be clearly more efficient to write:   
   >   
   > // [D]   
   > string s;   
   > for (int i=0; i!=n; ++i) {   
   > get_text(i, s); // void get_text(int, string&);   
   > to_lower(s);   
   > set_lower_text(i, s);   
   > }   
   >   
   > as now the allocated memory in the s buffer will be preserved between   
   > loop runs and it is very likely that we'll save on allocations.   
      
   Have you measured both variants? Which one was more efficient?   
      
      
   --   
    [ 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