Re: vector, but without the overhead of dynamic memory allocation
Goran <goran.pusic@gmail.com> wrote:
Once you've done a sufficiently big vector::reserve, there is no
additional allocation done by the vector. And yet, you say that this
improved things slightly. That is a clear-cut indication that
allocations done by the vector are not a problem (because there aren't
any!).
That would be incorrect: Of course std::vector is going to make at least
one allocation (eg. when you call reserve()).
If I understood correctly, the original poster's wrapper around a
static array is probably something like this:
template<typename Value_t, unsigned kCapacity>
class StaticArrayWrapper
{
Value_t valueArray[kCapacity];
public:
// Public member functions similar to the ones in std::vector
};
Notice that unlike std::vector, the above class makes no memory
allocations at all. This makes a huge difference when you instantiate
it many times. For example:
class A
{
std::vector<SomeType> data;
public:
A() { data.reserve(123); } // Makes an allocation
};
class B
{
StaticArrayWrapper<SomeType, 123> data; // Makes no allocations
};
std::vector<A> aVector(1000); // Makes 1001 allocations in total
std::vector<B> bVector(1000); // Makes 1 allocation in total
The difference is very significant.