Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficient push_back of classes and structs
    primarykey
    data
    text
    <p>I've seen my colleague do the second snippet quite often. Why is this? I've tried adding print statements to track the ctors and dtors, but both seem identical.</p> <pre><code> std::vector&lt;ClassTest&gt; vecClass1; ClassTest ct1; ct1.blah = blah // set some stuff ... vecClass1.push_back(ct1); </code></pre> <p></p> <pre><code> std::vector&lt;ClassTest&gt; vecClass2; vecClass2.push_back(ClassTest()); ClassTest&amp; ct2 = vecClass2.back(); ct2.blah = blah // set some stuff ... </code></pre> <p>PS. I'm sorry if the title is misleading.</p> <p><strong>Edit:</strong></p> <p>Firstly, thank you all for your responses.</p> <p>I've written a small application using <code>std::move</code>. The results are surprising to me perhaps because I've done something wrong ... would someone please explain why the "fast" path is performing significantly better.</p> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; #include &lt;boost/progress.hpp&gt; #include &lt;iostream&gt; const std::size_t SIZE = 10*100*100*100; //const std::size_t SIZE = 1; const bool log = (SIZE == 1); struct SomeType { std::string who; std::string bio; SomeType() { if (log) std::cout &lt;&lt; "SomeType()" &lt;&lt; std::endl; } SomeType(const SomeType&amp; other) { if (log) std::cout &lt;&lt; "SomeType(const SomeType&amp;)" &lt;&lt; std::endl; //this-&gt;who.swap(other.who); //this-&gt;bio.swap(other.bio); this-&gt;who = other.who; this-&gt;bio = other.bio; } SomeType&amp; operator=(SomeType&amp; other) { if (log) std::cout &lt;&lt; "SomeType::operator=()" &lt;&lt; std::endl; this-&gt;who.swap(other.who); this-&gt;bio.swap(other.bio); return *this; } ~SomeType() { if (log) std::cout &lt;&lt; "~SomeType()" &lt;&lt; std::endl; } void swap(SomeType&amp; other) { if (log) std::cout &lt;&lt; "Swapping" &lt;&lt; std::endl; this-&gt;who.swap(other.who); this-&gt;bio.swap(other.bio); } // move semantics SomeType(SomeType&amp;&amp; other) : who(std::move(other.who)) , bio(std::move(other.bio)) { if (log) std::cout &lt;&lt; "SomeType(SomeType&amp;&amp;)" &lt;&lt; std::endl; } SomeType&amp; operator=(SomeType&amp;&amp; other) { if (log) std::cout &lt;&lt; "SomeType::operator=(SomeType&amp;&amp;)" &lt;&lt; std::endl; this-&gt;who = std::move(other.who); this-&gt;bio = std::move(other.bio); return *this; } }; int main(int argc, char** argv) { { boost::progress_timer time_taken; std::vector&lt;SomeType&gt; store; std::cout &lt;&lt; "Timing \"slow\" path" &lt;&lt; std::endl; for (std::size_t i = 0; i &lt; SIZE; ++i) { SomeType some; some.who = "bruce banner the hulk"; some.bio = "you do not want to see me angry"; //store.push_back(SomeType()); //store.back().swap(some); store.push_back(std::move(some)); } } { boost::progress_timer time_taken; std::vector&lt;SomeType&gt; store; std::cout &lt;&lt; "Timing \"fast\" path" &lt;&lt; std::endl; for (std::size_t i = 0; i &lt; SIZE; ++i) { store.push_back(SomeType()); SomeType&amp; some = store.back(); some.who = "bruce banner the hulk"; some.bio = "you do not want to see me angry"; } } return 0; } </code></pre> <p>Output:</p> <pre><code>dev@ubuntu-10:~/Desktop/perf_test$ g++ -Wall -O3 push_back-test.cpp -std=c++0x dev@ubuntu-10:~/Desktop/perf_test$ ./a.out Timing "slow" path 3.36 s Timing "fast" path 3.08 s </code></pre>
    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.
 

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