Home » C# Programming

Vector vs. Array speed


I have a program that originally used c-style arrays, now I changed most of the arrays to use vectors, the problem is that the performance worsened by 60%.

Here are the steps in the code

1. Resize arrays based on input

2. Start Timer - to calculate time to run program

3. Do Calculations using vectors

4. End Timer


There was no resizing of the vectors, it just seems that if you are doing a lot of calculation on vectors they are slower then arrays by about 60%.  This seemed kind of high, does this make sense?

If vectors are this much slower I might try new/delete instead despite the drawbacks.





6 Answers Found


Answer 1

Did you test in Release mode? Did you turn off checked iterators by setting _SECURE_SCL and _SECUIRE_SCL_THROWS to 0?

Answer 2

Were your tests done with Debug builds or Release builds?

Were your tests done with checked iterators enabled or not?
(On by default.)

Checked Iterators

- Wayne

Answer 3



I just checked in "release" mode and the time  is the same.


I was worried for a moment.


Answer 4

I have tested the std::vector ad nauseum with various number theory projects. I have also used std::array and std::bitset and I can say this, std::vector, while imperfect gets the job done.

I built a wrapper around the std::vector to be able to do parallel things to it.

I went further into matrix formulas as well.

std:vector will give you reasonable performance.



Answer 5

What was the performance  drawback with the array.  I don't care about time  to resize  the  vector.  Arrays can't allocate deallocate memory and that seems like a huge drawback.

Answer 6

In my program, sieve.cpp I used char *, std::bitset, and std::vector as various containers and ran it.

It did not take long before the char* was dumped as its very profligate with memory. So I tried std::bitset, that choked at pow(2,32) so I was annoyed as usual.

So finally I tried std::vector<bool> which is the only construct available that can work with a cross off lise greater than pow(2,32)

So the sieve has the problem  of RAM consumption, using a single bit is as good as it gets.

Moving to parallel improved the performance  suggesting the CPU cache was helping.

The std::array is no better as its not as specialized as std::vector yet. I was also very disappointed over the limitations of the std::bitset which should have used size_t for its indexes.

Any programs offered on my site are 64-bit only. 32-bit need not apply.





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure