Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It depends on what you are comparing.</p> <p>Your benchmark measures both <em>setup</em> time and <em>access</em> times together. It's doubtless that <code>std::vector</code> has a more expensive setup time. This is because it needs to allocate memory, and then (by necessity of the standard) call default constructors on all the elements. Which for a POD type, means zeroing.</p> <p>So if you're trying to measure access times, then no your benchmark isn't accurate.</p> <p>Here's some numbers to digest:</p> <p><strong>Original Code:</strong></p> <pre><code>StartCounter(); vector&lt;int&gt; test1(vectorsize); for(int i=0; i&lt;vectorsize; i++){ test1[i] = 5; } cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl; </code></pre> <p><strong>Time: 444353.5206</strong></p> <hr> <p>Start timing <em>after</em> declaring and initializing the <code>vector</code>:</p> <pre><code>vector&lt;int&gt; test1(vectorsize); StartCounter(); for(int i=0; i&lt;vectorsize; i++){ test1[i] = 5; } cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl; </code></pre> <p><strong>Time: 15031.76101</strong></p> <hr> <p>And for the array:</p> <pre><code>StartCounter(); int test2[vectorsize]; for(int i=0; i&lt;vectorsize; i++){ test2[i] = 5; } cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl; </code></pre> <p><strong>Time: 38129.345</strong></p> <p>The times are about the same regardless of whether the declaration is timed. This is likely because stack allocation is done all at once upon entry to the function.</p> <hr> <p>Basically, the vector memory allocation and initialization is taking a disproportionate amount of time. But the actual loop is fast.</p> <p>I'll also note that your current benchmark framework is still sightly flawed. You only make one pass over each array. So cache-effects and lazy-allocation will be significant.</p> <p>The reason why the array is now slower is likely due to lazy-allocation. The array is allocated, but it hasn't been committed yet. Lazy allocation means that it is committed upon first access - which involves a page-fault and a context-switch to the kernel.</p> <hr> <p>Here's a fairer test with an outer loop to increase the benchmark time:</p> <pre><code>vector&lt;int&gt; test1(vectorsize); StartCounter(); for (int c = 0; c &lt; 10000; c++){ for(int i=0; i&lt;vectorsize; i++){ test1[i] = 5; } } cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl; </code></pre> <p><strong>Time: 227330454.6</strong></p> <pre><code>int test2[vectorsize]; memset(test2,0,sizeof(test2)); StartCounter(); for (int c = 0; c &lt; 10000; c++){ for(int i=0; i&lt;vectorsize; i++){ test2[i] = 5; } } cout &lt;&lt; GetCounter() &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; test2[0]; </code></pre> <p><strong>Time: 212286228.2</strong></p> <p>So no an array is <strong><em>NOT</em></strong> faster than a vector for steady-state access. It's just tricky to benchmark properly.</p>
 

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