Why do we need arrays when we have vectors in C++

时间:2022-01-14 22:27:02

Arrays allow us to use the __restrict__ keyword.  For instance, the following snippet, when compiled (with g++ -O3) and run on my system.

#include <cstdlib>

#include <iostream>

#include <vector>


#include <sys/time.h>

#include <unistd.h>


using namespace std;


void with_vectors(vector<int> a, vector<int> b) {

  for (int i = 0; i < a.size(); i++) {

    b[i] *= a[i];

  }

}


void with_arrays(int *__restrict__ a, int *__restrict__ b, int length) {

  for (int i = 0; i < length; i++) {

    b[i] *= a[i];

  }

}


long current_time_usecs() {

  struct timeval time;

  gettimeofday(&time, NULL);

  return time.tv_sec * 1000000 + time.tv_usec;

}


int main() {

  const int kLength = 5 * 1024 * 1024;


  int *array_0 = new int[kLength];

  int *array_1 = new int[kLength];


  for (int i = 0; i < kLength; i++) {

    array_0[i] = rand();

    array_1[i] = rand();

  }


  vector<int> vec_0(array_0, array_0 + kLength);

  vector<int> vec_1(array_1, array_1 + kLength);



  long begin_time = current_time_usecs();

  with_vectors(vec_0, vec_1);

  cout << "vectors took " << current_time_usecs() - begin_time << " usecs"

       << endl;


  begin_time = current_time_usecs();

  with_arrays(array_0, array_1, kLength);

  cout << "arrays took " << current_time_usecs() - begin_time << " usecs"

       << endl;

  return 0;

}

outputs

1
2
vectors took 23522 usecs
arrays took 5447 usecs



The code using arrays is significantly faster.

If you look at the assembly output (again, on g++ -O3), you'll see that the vector version is a simple scalar loop but the version using arrays is vectorized (ironic use of terms, I know :) ); and hence can make use of the processor's SIMD extensions.

Puritans may note that this isn't a fundamental theoretical issue, but a pragmatic one.  You may have super-sophisticated compilers which can "see" that with_vectors may be vectorized as well.