首页 > 编程语言 > 详细

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

时间:2014-02-01 13:50:50      阅读:448      评论:0      收藏:0      [点我收藏+]

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

bubuko.com,布布扣
#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;

}
bubuko.com,布布扣

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.

 

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

原文:http://www.cnblogs.com/zhouqz815/p/3536930.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!