figured it out. _SECURE_SCL must occur AFTER stdafx.h and BEFORE <vector>

I just added _SECURE_SCL=0 under "Preprocessor Definitions" in project properties and it worked.

11-25-2008

Daved

>> Looking at the dynamically allocated array version, I think that this would be closer to equivalent for the std::vector version <<

I don't think so (depending on the vector implementation). You're default constructing the vectors and then resizing them. The dynamic version just creates a single uninitialized pointer. I think constructing the vector with the initial size is closer. Of course it made little difference in my timing tests.

11-25-2008

laserlight

Quote:

Originally Posted by Daved

You're default constructing the vectors and then resizing them. The dynamic version just creates a single uninitialized pointer.

That's true. The default construction is likely to be very cheap, but still not as cheap as not doing anything at all. In this sense the visual similiarity is misleading.

11-25-2008

Daved

I did timing tests between three vector versions, including initializing in the intializer list, using reserve, and using push_back without reserve. There was no discernible differences in times at all, even when I reduced the amount of time spent on summing.

We're still talking about times and the tests, but does anybody have any thoughts on the results?

It seems to me that there are two things to notice. The first, as I mentioned before, is that proper use of a vector is not significantly slower than a dynamic array (and in at least one case is a tiny bit faster). Therefore trying to use a dynamic array for efficiency reasons is a premature optimization that is unlikely to have a noticeable impact on your application.

The second thing is that if efficiency is a concern, you should make sure to profile your application and be aware of things like Microsoft's added range checking that can have a significant effect on performance. Optimizations like defining _SECURE_SCL to 0 are the real things one should be looking for when making an efficient program.

11-25-2008

C_ntua

I ll agree that seeing these results it seem that the vector has almost the same performance as an array, so there is no real reason not to use it, since it benefits in many ways.

That was kind of expected though, since internally vector uses an array. I would find a far more interesting result if we would built a test which would also involve allocating memory. Like start from zero size, reallocate 10 spaces, sum, reallocate, sum etc etc.
Even more interesting would be if we had the vector add one one element, with push_back, but reallocate the array by some factor. In a way, reallocating an array increases its capacity, but adding an element in a vector increases its size and its capacity by some factor. So they would differ on this point, since the array will have the "right" capacity and the vector will increase its capacity automatically. The array should run faster, but if again the speeds are the same you can say:
1) If you have a X*Y vector and array the will run in almost the same time
2) If you have a vector and an array that both increase in a fixed size then again the will run the same

There is nothing else that you can do with an array, rather than resize it and iterate through it, so we will have a better view.

11-26-2008

laserlight

Quote:

Originally Posted by C_ntua

Even more interesting would be if we had the vector add one one element, with push_back, but reallocate the array by some factor. In a way, reallocating an array increases its capacity, but adding an element in a vector increases its size and its capacity by some factor.

Adding an element to a vector using push_back() increases its size by 1. Its capacity may or may not increase. If you want to duplicate such functionality with the dynamic array, then you need to keep track of the size, and will have to use placement new to avoid default constructing elements that are not yet in use. (Recall that vectors can store elements of types without a default constructor.)

Quote:

Originally Posted by C_ntua

So they would differ on this point, since the array will have the "right" capacity and the vector will increase its capacity automatically.

If you know the right capacity then the vector's reserve() would be available for use, so they need not differ (even though reserve() may only have a negligible effect anyway, as Daved has commented).