Choice of the most performant container (array)
<p>This is my little big question about containers, in particular, arrays.</p>

<p>I am writing a physics code that mainly manipulates a big (> 1 000 000) set of "particles" (with 6 <code>double</code> coordinates each). I am looking for the best way (in term of performance) to implement a class that will contain a container for these data and that will provide manipulation primitives for these data (e.g. instantiation, <code>operator[]</code>, etc.).</p>

<p>There are a few restrictions on how this set is used:</p>

<ul>
<li>its size is read from a configuration file and won't change during execution</li>
<li>it can be viewed as a big two dimensional array of N (e.g. 1 000 000) lines and 6 columns (each one storing the coordinate in one dimension)</li>
<li>the array is manipulated in a big loop, each "particle / line" is accessed and computation takes place with its coordinates, and the results are stored back for this particle, and so on for each particle, and so on for each iteration of the big loop.</li>
<li>no new elements are added or deleted during the execution</li>
</ul>

<p>First conclusion, as the access on the elements is essentially done by accessing each element one by one with <code>[]</code>, I think that I should use a normal dynamic array.</p>

<p>I have explored a few things, and I would like to have your opinion on the one that can give me the best performances.</p>

<p>As I understand there is no advantage to use a dynamically allocated array instead of a <code>std::vector</code>, so things like <code>double** array2d = new ..., loop of new, etc</code> are ruled out.</p>

<p>So is it a good idea to use <code>std::vector&lt;double&gt;</code> ?</p>

<p>If I use a <code>std::vector</code>, should I create a two dimensional array like <code>std::vector&lt;std::vector&lt;double&gt; &gt; my_array</code> that can be indexed like <code>my_array[i][j]</code>, or is it a bad idea and it would be better to use <code>std::vector&lt;double&gt; other_array</code> and acces it with <code>other_array[6*i+j]</code>.</p>

<p>Maybe this can gives better performance, especially as the number of columns is fixed and known from the beginning.</p>

<p>If you think that this is the best option, would it be possible to wrap this vector in a way that it can be accessed with a index operator defined as <code>other_array[i,j] // same as other_array[6*i+j]</code> without overhead (like function call at each access) ? </p>

<p>Another option, the one that I am using so far is to use Blitz, in particular <code>blitz::Array</code>:</p>

<pre><code>typedef blitz::Array&lt;double,TWO_DIMENSIONS&gt; store_t;
store_t my_store;
</code></pre>

<p>Where my elements are accessed like that: <code>my_store(line, column);</code>.</p>

<p>I think there are not much advantage to use Blitz in my case because I am accessing each element one by one and that Blitz would be interesting if I was using operations directly on array (like matrix multiplication) which I am not.</p>

<p>Do you think that Blitz is OK, or is it useless in my case ?</p>

<p>These are the possibilities I have considered so far, but maybe the best one I still another one, so don't hesitate to suggest me other things.</p>

<p>Thanks a lot for your help on this problem !</p>

<p><strong>Edit:</strong></p>

<p>From the very interesting answers and comments bellow a good solution seems to be the following:</p>

<ul>
<li>Use a structure <code>particle</code> (containing 6 doubles) or a static array of 6 doubles (this avoid the use of two dimensional dynamic arrays)</li>
<li>Use a <code>vector</code> or a <code>deque</code> of this <code>particle</code> structure or array. It is then good to traverse them with iterators, and that will allow to change from one to another later.</li>
</ul>

<p>In addition I can also use a <code>Blitz::TinyVector&lt;double,6&gt;</code> instead of a structure.</p>
