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

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.

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。