C ++阵VS向量性能测试说明 [英] C++ Array vs Vector performance test explanation

查看:94
本文介绍了C ++阵VS向量性能测试说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了量化C ++中的C-状阵列和载​​体的性能差异,我写了这个小程序。的https://github.com/rajatkhanduja/Benchmarks/blob/master/C%2B%2B/vectorVsArray.cpp

In order to quantify the difference in performance of a C-like array and Vectors in C++, I wrote this little program. https://github.com/rajatkhanduja/Benchmarks/blob/master/C%2B%2B/vectorVsArray.cpp

要比较它们的共同点,我决定测试无论是随机和顺序访问。我加了迭代器,只需将它们进行比较,同时(但不是问题的主要论点集中在什么)。

To compare them on common grounds, I decided to test both for random and sequential access. I added iterators, just to compare them as well (but that is not what the question focusses on).

结果,对于一个64位的Linux机器有7.7 GB的RAM与100万的数组/矢量大小如下: -

The results, for a 64-bit Linux machine with 7.7 GB RAM with on an array/vector size of 1 million are as follows:-


  • 拍摄时间写入阵列。 :12.0378毫秒

  • 所需的时间从连续阵列读取。 :2.48413毫秒

  • 所需的时间从阵列读取随机。 :37.3931毫秒

  • 拍摄时间写入动态数组。 :11.7458毫秒

  • 所需的时间从动态数组按顺序阅读。 :2.85107毫秒

  • 所需的时间从动态数组随机读取。 :36.0579毫秒

  • 所需的时间编写使用索引来向量。 :11.3909毫秒

  • 拍摄时使用指数从载体阅读,顺序。 :4.09106毫秒

  • 拍摄时使用指数从载体读取,随机。 :39毫秒

  • 所需的时间编写使用迭代器向量。 :24.9949毫秒

  • 拍摄时间使用迭代器从载体阅读。 :18.8049毫秒

的载体的大小被设定在初始化时和没有改变,所以没有载体的大小调整(在程序断言有助于验证)。的时间不包括任何的静态分配阵列的初始化时间,动态分配的数组或载体。

The vector's size is set at the time of initialization and not altered, so there is no resizing of the vector (the assertion in the program helps verify that). The times don't include the initialization times of any of the statically allocated array, dynamically allocated array or the vector.

据统计,写入向量的时间比该阵列但从向量读取时间的较小两倍该阵列的

According to the statistics, the time to write to Vector is lesser than that of array but the time to read from vector is twice as much as that of array.

所不同的是pretty小,但有一个解释,为什么有性能差异?是不是有什么毛病的考验吗?我希望双方以相同的速度执行。这个试验的重复显示相同的趋势。

The difference is pretty small, but is there an explanation as to why there is a performance difference ? Is there something wrong with the test ? I expected both to perform at the same speed. The repetition of this test shows the same trend.

在code:

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sys/time.h>
#include <cassert>

#define ARR_SIZE 1000000

using std::string;

void printtime (struct timeval& start, struct timeval& end, string str);   

int main (void)
{
  int arr[ARR_SIZE];
  int tmp;
  struct timeval start, stop;

  srand (time (NULL));

  /* Writing data to array */
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    arr[i] = rand();
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to write to array."));

  /* Reading data from array */
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = arr[i];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from array sequentially."));

  /* Reading data from array randomly*/
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = arr[rand() % ARR_SIZE];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from array randomly."));


  int *darr = (int *) calloc (sizeof (int), ARR_SIZE);  

  /* Writing data to array */
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    darr[i] = rand();
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to write to dynamic array."));

  /* Reading data from array */
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = darr[i];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from dynamic array sequentially."));

  /* Reading data from dynamic array randomly*/
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = darr[rand() % ARR_SIZE];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from dynamic array randomly."));

  std::vector<int> v(ARR_SIZE);
  assert (v.capacity() == ARR_SIZE);

  /* Writing to vector using indices*/
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    v[i] = rand();
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to write to vector using indices."));
  assert (v.capacity() == ARR_SIZE);

  /* Reading from vector using indices*/
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = v[i];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from vector using indices, sequentially."));

  /* Reading data from dynamic array randomly*/
  gettimeofday (&start, NULL);
  for (int i = 0; i < ARR_SIZE; i++)
  {
    tmp = v[rand() % ARR_SIZE];
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from vector using indices, randomly."));

  std::vector<int> v2(ARR_SIZE);

  /* Writing to vector using iterators*/
  gettimeofday (&start, NULL);
  std::vector<int>::iterator itr, itr_end;
  for (itr = v2.begin(), itr_end = v2.end(); itr != itr_end; itr++)
  {
    *itr = rand();
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to write to vector using iterators."));


  /* Reading from vector using iterators*/
  gettimeofday (&start, NULL);
  for (itr = v2.begin(), itr_end = v2.end(); itr != itr_end; itr++)
  {
    tmp = *itr;
  }
  gettimeofday (&stop, NULL);
  printtime (start, stop, string ("Time taken to read from vector using iterators."));

  return 0;
}

void printtime (struct timeval& start, struct timeval& end, string str)
{
  double start_time, end_time, diff;

  start_time = ((start.tv_sec) * 1000 + start.tv_usec/1000.0);
  end_time   = ((end.tv_sec) * 1000 + end.tv_usec/1000.0);
  diff = end_time - start_time;

  std::cout << str << " : " << diff << " ms" << std::endl;
}


修改

作为意见认为,这里是详细信息: -

As suggested in comments, here is more information :-


  • 编译器: - G ++ - 4.5.2

  • 标记: - 无(即默认值)

  • 优化: - 无(我想测试在通常设置的行为,因为从未使用的变量TMP优化可以改变程序的行为,例如,读取向量/阵列的步骤可以完全跳过或者降低到刚刚过去的分配。至少这是我的理解)。

推荐答案

当然不是一个明确的答案,但你正在写在一个循环中的变量,这意味着编译器可以轻易地猜到最后的结果应该是什么顺序读取,从而优化了环路的路程。因为它显然不这样做,我认为是没有优化,这肯定不利于迭代器的方法。其他数字都分不出来采取的结论。

Certainly not a definitive answer, but you are writing in a loop to a variable, meaning that a compiler can easily guess what the end result should be for sequential reading, thus optimizing the loop away. Since it's obviously not doing this, I assume that there is no optimization which definitely doesn't favor the iterator approach. The other numbers are too close to take conclusions.

这篇关于C ++阵VS向量性能测试说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆