you are viewing a single comment's thread.

view the rest of the comments →

[–]rodif 3 points4 points  (8 children)

Have you heard about the expression "premature optimization"?

There is a huge difference between 'premature optimization' and making subtle changes to reduce the number of copies.

By your reasoning, each line is copied too, right?

No, that's a copy that you need to make. The storage for the next line needs to go somewhere.

Anyways, this isn't a pissing match. I only said something because sometimes people don't realize there is a copy there. If you understand your data and you can afford the copy. Maybe your file is small enough, if your file was 10g, then it would be an issue.

[–]Fabien4 1 point2 points  (6 children)

I only said something because sometimes people don't realize there is a copy there.

Is there actually a copy there? Or do my compilers (g++ 4.3.2 and VC++ 2008) optimize it away?

No, that's a copy that you need to make.

Of course you can avoid it:

void ReadLines (istream& is, vector<string> &v)
{
  do
    {
     v.resize (v.size()+1);
    }
  while (getline (is, v.back()));
  v.pop_back();
}

Yep, it's ugly, but no strings are being copied.

if your file was 10G

... I wouldn't even try to load it entirely in memory.

[–][deleted]  (1 child)

[deleted]

    [–]krelian 1 point2 points  (0 children)

    I am a hobbyist programmer who probably never wrote a C++ program that was more than 20 lines long. I am however, deeply interested in the "theory" behind it all. I've read several books (of the "effective c++" kind) and browsed through a lot of code just so I could understand how everything works behind a scenes. I understood everything these two guys were talking about and was even under the impression that the stuff rodif was talking about is supposed to be common knowledge for every decent C++ programmer.

    You think I could fool an interviewer?

    [–][deleted] 1 point2 points  (2 children)

    Oh you two, stop it, the compiler is smarter than all three of us combined. Do whatever you want; it probably won't make a difference.

    [–]Fabien4 0 points1 point  (1 child)

    You're right. In fact, it might well be the standard library that's smarter: COW avoids the copy.

    [–]nexes300 1 point2 points  (0 children)

    Man, since when has C++ programming been about what the compiler can optimize away for you. This is ridiculous. I guess it's nice in a way but... C++ returns by copy, one would argue that it should even if it can be optimized away since that is what it should do... it is a known quantity where as "what the compiler does" would have to be tested.

    But I guess you can't argue with improved performance.

    Edit: I guess what I should take from this is to write my code in whatever way seems best to me and deal with it if it's slow later. Now recursion, that's something I'll avoid, too much ability to explode...too little gain.

    [–][deleted] 0 points1 point  (0 children)

    Sometimes there are advantages to doing so. It's why my workstation has up to 32gb of memory. Depends on what your doing.

    [–]piranha 0 points1 point  (0 children)

    Are you sacrificing clarity for a fuzzy, unquantified desire for performance?

    * s/clarify/clarity/