Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I simply did a quick performance measurement with the following code and </p> <pre><code>v1.insert( v1.end(), v2.begin(), v2.end() ); </code></pre> <p>seems to be the right choice (as already stated above). Nevertheless, you find the reported performance below.</p> <p>Test code:</p> <pre><code>#include &lt;vector&gt; #include &lt;string&gt; #include &lt;boost/timer/timer.hpp&gt; //============================================================================== // //============================================================================== /// Returns a vector containing the sequence [ 0, ... , n-1 ]. inline std::vector&lt;int&gt; _range(const int n) { std::vector&lt;int&gt; tmp(n); for(int i=0; i&lt;n; i++) tmp[i] = i; return tmp; } void test_perf_vector_append() { const vector&lt;int&gt; testdata1 = _range(100000000); const vector&lt;int&gt; testdata2 = _range(100000000); vector&lt;int&gt; testdata; printf("--------------------------------------------------------------\n"); printf(" METHOD: push_back()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; for(size_t i=0; i&lt;testdata2.size(); i++) { testdata.push_back(testdata2[i]); } } printf("--------------------------------------------------------------\n"); printf(" METHOD: reserve() + push_back()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; testdata.reserve(testdata.size() + testdata2.size()); for(size_t i=0; i&lt;testdata2.size(); i++) { testdata.push_back(testdata2[i]); } } printf("--------------------------------------------------------------\n"); printf(" METHOD: insert()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; testdata.insert( testdata.end(), testdata2.begin(), testdata2.end() ); } printf("--------------------------------------------------------------\n"); printf(" METHOD: reserve() + insert()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; testdata.reserve( testdata.size() + testdata.size() ); testdata.insert( testdata.end(), testdata2.begin(), testdata2.end() ); } printf("--------------------------------------------------------------\n"); printf(" METHOD: copy() + back_inserter()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; testdata.reserve(testdata.size() + testdata2.size()); copy(testdata2.begin(), testdata2.end(), back_inserter(testdata)); } printf("--------------------------------------------------------------\n"); printf(" METHOD: reserve() + copy() + back_inserter()\n"); printf("--------------------------------------------------------------\n"); testdata.clear(); { vector&lt;int&gt;().swap(testdata); } testdata = testdata1; { boost::timer::auto_cpu_timer t; testdata.reserve(testdata.size() + testdata2.size()); copy(testdata2.begin(), testdata2.end(), back_inserter(testdata)); } } </code></pre> <p>With Visual Studio 2008 SP1, x64, Release mode, /O2 /LTCG the output is as follows:</p> <pre><code>-------------------------------------------------------------- METHOD: push_back() -------------------------------------------------------------- 0.933077s wall, 0.577204s user + 0.343202s system = 0.920406s CPU (98.6%) -------------------------------------------------------------- METHOD: reserve() + push_back() -------------------------------------------------------------- 0.612753s wall, 0.452403s user + 0.171601s system = 0.624004s CPU (101.8%) -------------------------------------------------------------- METHOD: insert() -------------------------------------------------------------- 0.424065s wall, 0.280802s user + 0.140401s system = 0.421203s CPU (99.3%) -------------------------------------------------------------- METHOD: reserve() + insert() -------------------------------------------------------------- 0.637081s wall, 0.421203s user + 0.218401s system = 0.639604s CPU (100.4%) -------------------------------------------------------------- METHOD: copy() + back_inserter() -------------------------------------------------------------- 0.743658s wall, 0.639604s user + 0.109201s system = 0.748805s CPU (100.7%) -------------------------------------------------------------- METHOD: reserve() + copy() + back_inserter() -------------------------------------------------------------- 0.748560s wall, 0.624004s user + 0.124801s system = 0.748805s CPU (100.0%) </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.
    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