title: For C++ Vector3 utility class implementations, is array faster than struct and class?
body: <p>just out of curiosity, I implemented vector3 utilities in 3 ways: array (with a typedef), class and struct</p>
  
  <p>This is the array implementation:</p>
  
  <pre><code>typedef float newVector3[3];
  
  namespace vec3{
      void add(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3);
      void subtract(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3);
      void dot(const newVector3&amp; first, const newVector3&amp; second, float&amp; out_result);
      void cross(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3);
      }
  
      // implementations, nothing fancy...really
  
       void add(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3)
  
      {
          out_newVector3[0] = first[0] + second[0];
          out_newVector3[1] = first[1] + second[1];
          out_newVector3[2] = first[2] + second[2];
      }
  
      void subtract(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3){
          out_newVector3[0] = first[0] - second[0];
          out_newVector3[1] = first[1] - second[1];
          out_newVector3[2] = first[2] - second[2];
      }
  
      void dot(const newVector3&amp; first, const newVector3&amp; second, float&amp; out_result){
          out_result = first[0]*second[0] + first[1]*second[1] + first[2]*second[2];
      }
  
      void cross(const newVector3&amp; first, const newVector3&amp; second, newVector3&amp; out_newVector3){
          out_newVector3[0] = first[0] * second[0];
          out_newVector3[1] = first[1] * second[1];
          out_newVector3[2] = first[2] * second[2];
      }
  }
  </code></pre>
  
  <p>And a class implementation:</p>
  
  <pre><code>class Vector3{
  private:
      float x;
      float y;
      float z;
  
  public:
      // constructors
      Vector3(float new_x, float new_y, float new_z){
          x = new_x;
          y = new_y;
          z = new_z;
      }
  
      Vector3(const Vector3&amp; other){
          if(&amp;other != this){
              this-&gt;x = other.x;
              this-&gt;y = other.y;
              this-&gt;z = other.z;
          }
      }
  }
  </code></pre>
  
  <p>Of course, It contains other functionalities that usually appears in a Vector3 class.</p>
  
  <p>And finally, a struct implementation:</p>
  
  <pre><code>struct s_vector3{
      float x;
      float y;
      float z;
  
      // constructors
      s_vector3(float new_x, float new_y, float new_z){
          x = new_x;
          y = new_y;
          z = new_z;
      }
  
      s_vector3(const s_vector3&amp; other){
          if(&amp;other != this){
              this-&gt;x = other.x;
              this-&gt;y = other.y;
              this-&gt;z = other.z;
          }
      }
  </code></pre>
  
  <p>Again, I omitted some other common Vector3 functionalities.
  Now, I let all three of them create 9000000 new objects, and do 9000000 times of cross product(I wrote a huge chunk of data data to cache after one of them finishes, to avoid cache help them out).</p>
  
  <p>Here is the test code:</p>
  
  <pre><code>const int K_OPERATION_TIME = 9000000;
  const size_t bigger_than_cachesize = 20 * 1024 * 1024;
  
  void cleanCache()
  {
      // flush the cache
      long *p = new long[bigger_than_cachesize];// 20 MB
      for(int i = 0; i &lt; bigger_than_cachesize; i++)
      {
         p[i] = rand();
      }
  }
  
  int main(){
  
      cleanCache();
      // first, the Vector3 struct
      std::clock_t start;
      double duration;
  
      start = std::clock();
  
      for(int i = 0; i &lt; K_OPERATION_TIME; ++i){
          s_vector3 newVector3Struct = s_vector3(i,i,i);
          newVector3Struct = s_vector3::cross(newVector3Struct, newVector3Struct);
      }
  
      duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
      printf("The struct implementation of Vector3 takes %f seconds.\n", duration);
  
      cleanCache();
      // second, the Vector3 array implementation
      start = std::clock();
  
      for(int i = 0; i &lt; K_OPERATION_TIME; ++i){
          newVector3 newVector3Array = {i, i, i};
          newVector3 opResult;
          vec3::cross(newVector3Array, newVector3Array, opResult);
      }
  
      duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
      printf("The array implementation of Vector3 takes %f seconds.\n", duration);
  
      cleanCache();
      // Third, the Vector3 class implementation
      start = std::clock();
  
      for(int i = 0; i &lt; K_OPERATION_TIME; ++i){
          Vector3 newVector3Class = Vector3(i,i,i);
          newVector3Class = Vector3::cross(newVector3Class, newVector3Class);
      }
  
      duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
      printf("The class implementation of Vector3 takes %f seconds.\n", duration);
  
  
      return 0;
  }
  </code></pre>
  
  <p>The result is astonishing.</p>
  
  <p><code>struct</code> and <code>class</code> implementations finishes the task around 0.23 seconds,
  whereas <code>array</code> implementation only takes 0.08 seconds!</p>
  
  <p>If array does have significant performance advantage like this, though its syntax would be ugly, it's worth using in a lot of cases.</p>
  
  <p>So I really want to make sure, is this what supposed to happen? Thanks!</p>