Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Element access time in a typical implementation of a <code>std::vector</code> is the same as element access time in an ordinary array available through a <em>pointer object</em> (i.e. a <em>run-time</em> pointer value)</p> <pre><code>std::vector&lt;int&gt; v; int *pa; ... v[i]; pa[i]; // Both have the same access time </code></pre> <p>However, the access time to an element of an array available as an <em>array object</em> is better than both of the above accesses (equivalent to access through a <em>compile-time</em> pointer value)</p> <pre><code>int a[100]; ... a[i]; // Faster than both of the above </code></pre> <p>For example, a typical read access to an <code>int</code> array available through a run-time pointer value will look as follows in the compiled code on x86 platform</p> <pre><code>// pa[i] mov ecx, pa // read pointer value from memory mov eax, i mov &lt;result&gt;, dword ptr [ecx + eax * 4] </code></pre> <p>Access to vector element will look pretty much the same.</p> <p>A typical access to a local <code>int</code> array available as an array object will look as follows</p> <pre><code>// a[i] mov eax, i mov &lt;result&gt;, dword ptr [esp + &lt;offset constant&gt; + eax * 4] </code></pre> <p>A typical access to a global <code>int</code> array available as an array object will look as follows</p> <pre><code>// a[i] mov eax, i mov &lt;result&gt;, dword ptr [&lt;absolute address constant&gt; + eax * 4] </code></pre> <p>The difference in perfromance arises from that extra <code>mov</code> instruction in the first variant, which has to make an extra memory access.</p> <p>However, the difference is negligible. And it is easily optimized to the point of being exactly the same in multiple-access context (by loading the target address in a register). </p> <p>So the statement about "arrays being a bit faster" is correct in narrow case when the array is accessible directly through the array object, not through a pointer object. But the practical value of that difference is virtually nothing.</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