A little exersize I did recently which essentially compares the speed of arrays versus vectors.
I test the creation, usage, and deletion speeds of vectors and arrays and the results are printed on screen.
Long story short, when programming in debug mode there is a HUGE difference between arrays (faster) and vectors (slower) but when programming in release mode that difference is mostly wiped out with arrays only having an extremely small advantage.
Considering all of the other advantages that you get with vectors there should be no reason why people don't use them.
Anyway, just thought I would post to help someone else out there with learning C++.
You should be able to paste the below code in an empty file, compile, and run.
If you know how many items will be added to the vector you should use reserve to reduce the overhead of the following push_backs.
VectorOfClasses.reserve(NumberOfClassesToCreate);
In this case you don't really need the push_backs, you could simply add all elements in one function call, using resize.
VectorOfClasses.resize(NumberOfClassesToCreate);
The at function do bounds checking so it's not fair to compare it with an array. A more fair comparison would be to use vector's operator[].
Your test is broken. You need to do something to prevent the compiler from optimizing away too much. When I run your program the timings are about the same no matter what value I give to NumberOfClassesToCreate.
You aren't really measuring the time to create & destroy a vector vs. an array for 2 reasons. First, because your vector/array contains a class with a destructor, most of the time is probably taken up by destroying the items *in* the container, not the container itself. Second, in my opinion you aren't really using arrays, you're using pointers to dynamically allocated arrays.
So you might want to try another set of tests where the array size is known at compile time. If you use a fix-length array of int, I think you'll find that the time to create & destroy the array is very small compared to a vector.
On your two points; I wanted to measure usage of real world classes with constructors/destructors and dynamically created arrays.
I hope not to start an argument by saying this but most of the time the arrays you want/need to use don't have fixed sizes. Therefore IMHO it is important to test as you are going to use them vs testing in an ideal scenario where arrays have fixed sizes.
That being said; I altered the example to include a test of fixed array sizes.
I wanted to measure usage of real world classes with constructors/destructors and dynamically created arrays.
These are certainly real-world examples, but I worry that the differences between arrays and vectors will be dwarfed by the time required to construct/destroy the items in the container. On the other hand, that, in itself, may be instructive. In other words, the choice of the container may not matter much if the contained items are at all complex.
Most of the time the arrays you want/need to use don't have fixed sizes.
That depends on the application of course. I think the important thing is to understand exactly what you're comparing so you know when to apply the results.
Cool stuff. Thanks for doing this and showing the results.