简体   繁体   English

矢量初始化比数组慢...为什么?

[英]Vector initializing slower than array…why?

I tried 2 things: (pseudo code below) 我尝试了两件事:(下面的伪代码)

int arr[10000];
for (int i = 0; i < 10000; i++)
{
   for (int j = 0; j < 10000; j++)
   {
       arr[j] = j;
   }
}

and

vector<int> arr(10000);
for (int i = 0; i < 10000; i++)
{
   for (int j = 0; j < 10000; j++)
   {
       arr[j] = j;
   }
}

I ran both the programs and timed it using the "time" shell command. 我运行了两个程序并使用“time”shell命令对其进行了计时。 Program 1 runs in 5 seconds, program 2 runs in 30 seconds. 程序1在5秒内运行,程序2在30秒内运行。 I ran both programs with compiler optimization turned on, and both programs ran in about the same time (0.38s). 我打开了编译器优化的两个程序,两个程序大约在同一时间运行(0.38s)。 I am confused by these results. 我对这些结果感到困惑。 Can someone please explain to me why this is happening? 有人可以向我解释为什么会这样吗?

Thanks! 谢谢!

For the template, subscripting is done with operator[]. 对于模板,下标是使用operator []完成的。 With optimization turned off, that'll usually be generated as a real function call, adding a lot of overhead to something as simple as subscripting into an array. 关闭优化后,通常会将其作为一个真正的函数调用生成,这会增加很多开销,就像订阅数组一样简单。 When you turn on optimization, it's generated inline, removing that overhead. 当您启用优化时,它会内联生成,从而消除了这种开销。

In debugging mode, implementations of std::vector provide a lot of run-time checking for ease of use. 在调试模式下, std::vector提供了大量的运行时检查以方便使用。 This checking is not available for native arrays. 此检查不适用于本机阵列。 For example, in VC2008, if you compile your vector example in debugging mode, there will be range-checking even in the case of operator[] . 例如,在VC2008中,如果在调试模式下编译vector示例, 即使operator[]的情况下也会进行range-checking

如果您的非优化矢量实现正在执行边界检查,那么这将解决差异。

These are good answers, but there's a quick way you can find out for yourself. 这些都是很好的答案,但有一个快速的方法,你可以找到自己。

You're seeing a 6-to-1 difference in performance, right? 你看到性能上有6比1的差异吧? Just run the slow one and hit the "pause" button. 只需运行慢速按钮并点击“暂停”按钮即可。 Then look at the call stack. 然后看一下调用堆栈。 The probability is 5 out of 6 (83%) that you will see exactly how it is spending those 25 extra seconds. 概率是6分(83%)中的5分,您将看到它们如何花费这25秒的额外时间。 Do it several times to get as much insight as you want. 做几次以获得您想要的洞察力。

For the optimized case, do the same with program 1. Since it is 13 times slower than the optimized program, you will see the reason why on each "pause", with probability 12/13 = 92%. 对于优化的情况,对程序1执行相同的操作。由于它比优化程序慢13倍,您将看到每个“暂停”的原因,概率为12/13 = 92%。

That is an application of this technique . 这是这种技术的应用。

because when you write vector arr(10000); 因为当你写矢量arr(10000); you create an object, call it's functions... when and it will be slower than whe you create int arr[10000]; 你创建一个对象,调用它的函数......什么时候它会比你创建int arr [10000]时慢;

In your examples, the array is on the stack. 在您的示例中,阵列位于堆栈中。 Accessing data in the array involves accessing data on the stack. 访问阵列中的数据涉及访问堆栈上的数据。 That's fast. 那很快。

On the other hand, while the vector is on the stack, the data for a std::vector is allocated somewhere else (by default it's allocated on the heap via std::allocator ). 另一方面,当vector在堆栈上时, std::vector的数据被分配到其他地方(默认情况下,它通过std::allocator在堆上std::allocator )。 Accessing data in the vector involves accessing data on the heap. 访问vector数据涉及访问堆上的数据。 That's much slower than accessing data on the stack. 这比访问堆栈上的数据要慢得多。

You get something for the performance penalty, though. 但是,你会得到一些性能损失的东西。 std::vector is growable, while a regular array is not. std::vector是可增长的,而常规数组则不是。 Also, the size of a std::vector does not have to be compile time constant, while the size of an array on the stack does. 此外, std::vector的大小不必是编译时常量,而堆栈上的数组大小也是如此。 A heap-allocated array (via operator new[] ) does not have to be a compile-time constant. 堆分配的数组(通过operator new[] )不必是编译时常量。 If you compare a heap allocated array with a std::vector you'll find the performance is much closer. 如果将堆分配的数组与std::vector您会发现性能更接近。

int* arr = new int[10000];
for (int i = 0; i < 10000; i++)
{
   for (int j = 0; j < 10000; j++)
   {
       arr[j] = j;
   }
}

delete[] arr; // std::vector does this for you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM