title: Array of Structs are always faster than Structs of arrays?
body: <p>I was wondering if the data layout <code>Structs of Arrays</code> (<strong>SoA</strong>) is always faster than an <code>Array of Structs</code> (<strong>AoS</strong>) or <code>Array of Pointers</code> (<strong>AoP</strong>) for problems with inputs that only fits in <code>RAM</code> programmed in <code>C/JAVA</code>.</p>
  
  <p>Some days ago I was improving the performance of a Molecular Dynamic algorithm (in C), summarizing in this algorithm it is calculated the force interaction among particles based on their force and position.</p>
  
  <p>Original the particles were represented by a struct containing 9 different doubles, 3 for particles forces (Fx,Fy,Fz) , 3 for positions and 3 for velocity. The algorithm had an array containing pointers to all the particles (<strong>AoP</strong>). I decided to change the layout from <strong>AoP</strong> to <strong>SoA</strong> to improve the cache use.</p>
  
  <p>So, now I have a Struct with 9 array where each array stores forces, velocity and positions (x,y,z) of each particle. Each particle is accessed by it own array index.</p>
  
  <p>I had a gain in performance (for an input that only fits in RAM) of about <strong>1.9x</strong>, so I was wondering if typically changing from <strong>AoP</strong> or <strong>AoS</strong> to <strong>SoA</strong> it will always performance better, and if not in which types of algorithms this do not occurs.</p>