Вектор, инициализирующий медленнее, чем массив …, почему?

b >= (a+p) && a>=1

даже b >= p избыточно, поскольку это будет всегда иметь место для a >= 1

8
задан Aishwar 23 October 2009 в 06:50
поделиться

6 ответов

For the template, subscripting is done with 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.

16
ответ дан 3 November 2019 в 13:09
поделиться

In debugging mode, implementations of std::vector provide a lot of run-time checking for ease of use. 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[].

8
ответ дан 3 November 2019 в 13:09
поделиться

, потому что когда вы пишете вектор arr (10000); вы создаете объект, вызываете его функции ... когда и это будет медленнее, чем когда вы создаете int arr [10000];

0
ответ дан 3 November 2019 в 13:09
поделиться

Если ваша неоптимизированная реализация вектора выполняет проверку границ, это может объяснить расхождение.

5
ответ дан 3 November 2019 в 13:09
поделиться

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? 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. 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%.

That is an application of this technique.

4
ответ дан 3 November 2019 в 13:09
поделиться

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). Accessing data in the vector involves accessing data on the heap. 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. 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. A heap-allocated array (via operator new[]) does not have to be a compile-time constant. If you compare a heap allocated array with a std::vector you'll find the performance is much closer.

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
0
ответ дан 3 November 2019 в 13:09
поделиться
Другие вопросы по тегам:

Похожие вопросы: