Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your array arr is allocated on the stack, i.e., the compiler has calculated the necessary space at compile time. At the beginning of the method, the compiler will insert an assembler statement like</p> <pre><code>sub esp, 10000*10000*sizeof(int) </code></pre> <p>which means the stack pointer (<code>esp</code>) is decreased by <code>10000 * 10000 * sizeof(int)</code> bytes to make room for an array of 10000<sup>2</sup> integers. This operation is almost instant. </p> <p>The vector is heap allocated and heap allocation is much more expensive. When the vector allocates the required memory, it has to ask the operating system for a contiguous chunk of memory and the operating system will have to perform significant work to find this chunk of memory.</p> <p>As Andreas says in the comments, all your time is spent in this line:</p> <pre><code>vector&lt;int&gt; v(size*size); </code></pre> <p>Accessing the vector inside the loop is just as fast as for the array.</p> <p>For an additional overview see e.g.</p> <ul> <li><p>[<a href="https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap][1]">What and where are the stack and heap?</a></p></li> <li><p>[<a href="http://computer.howstuffworks.com/c28.htm][2]" rel="nofollow noreferrer">http://computer.howstuffworks.com/c28.htm][2]</a></p></li> <li><p>[<a href="http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html][3]" rel="nofollow noreferrer">http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html][3]</a></p></li> </ul> <p><strong>Edit:</strong></p> <p>After all the comments about performance optimizations and compiler settings, I did some measurements this morning. I had to set <code>size=3000</code> so I did my measurements with roughly a tenth of the original entries. All measurements performed on a 2.66 GHz Xeon:</p> <ol> <li><p>With debug settings in Visual Studio 2008 (no optimization, runtime checks, and debug runtime) the vector test took 920 ms compared to 0 ms for the array test. </p> <p>98,48 % of the total time was spent in <code>vector::operator[]</code>, i.e., the time was indeed spent on the runtime checks. </p></li> <li><p>With full optimization, the vector test needed 56 ms (with a tenth of the original number of entries) compared to 0 ms for the array. </p> <p>The vector ctor required 61,72 % of the <em>total</em> application running time. </p></li> </ol> <p>So I guess everybody is right depending on the compiler settings used. The OP's timing suggests an optimized build or an STL without runtime checks. </p> <p>As always, the morale is: profile first, optimize second. </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. 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