Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Of course, and everyone says this, don't optimize prematurely. Don't bother with this until and unless you prove a) that your program goes too slowly, and b) it would go faster if you didn't copy so much data.</p></li> <li><p>If your program design requires you to hold multiple simultaneous copies of the data, there is nothing you can do. You just have to bite the bullet and copy the data. No, implementing a custom copy constructor and custom assignment operator won't make it go faster.</p></li> <li><p>If your program doesn't require multiple simultaneous copies of this data, then you do have a couple of tricks to reduce the number of copies you perform.</p></li> </ol> <p><strong>Instrument your copy methods</strong> If it were me, the first thing I would do, even before trying to improve anything, is to count the number of times my copy methods were invoked.</p> <pre><code>class Foo { private: static int numberOfConstructors; static int numberofCopyConstructors; static int numberofAssignments; Foo() { ++numberOfConstructors; ...; } Foo(const Foo&amp; f) : vec1(f.vec1), vec2(f.vec2), bimap(f.bimap) { ++numberOfCopyConstructors; ...; } Foo&amp; operator=(const Foo&amp; f) { ++numberOfAssignments; ...; } }; </code></pre> <p>Run your program with and without your improvements. Print out the value of those static members to see if your changes had any effect.</p> <p><strong>Avoid assignments in function calls by using references</strong> If you pass objects of type Foo to functions, consider if you can do it by reference. If you don't change the passed copy, passing it by const reference is a no-brainer.</p> <pre><code>// WAS: extern SomeFuncton(Foo f); // EASY change -- if this compiles, you know that it is correct extern SomeFunction(const Foo&amp; f); // HARD change -- you have to examine your code to see if this is safe extern SomeFunction(Foo&amp; f); </code></pre> <p><strong>Avoid copies by using Foo::swap</strong> If you use the copy methods (either explicitly or implicitly) a lot, consider whether the assigned-from item could give up its data, rather than copying it.</p> <pre><code>// Was: vectorOfFoo.push_back(myFoo); // maybe faster: vectorOfFoo.push_back(Foo()); vectorOfFoo.back().swap(myFoo); // Was: newFoo = oldFoo; // maybe faster newfoo.swap(oldFoo); </code></pre> <p>Of course, this only works if <code>myFoo</code> and <code>oldFoo</code> no longer need access to their data. And, you have to implement <code>Foo::swap</code></p> <pre><code>void Foo::swap(Foo&amp; old) { std::swap(this-&gt;vec1, old.vec1); std::swap(this-&gt;vec2, old.vec2); ... } </code></pre> <p>Whatever you do, measure your program before <em>and</em> after your change. Measure the number of times your copy methods are invoked, and the total time improvement in your program.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload