Juha Nieminen said:
The point was to demonstrate the speed of memory allocation,
not the speed of std::vector vs. std::set. I could just as well have used
std::list, std::map or outright raw 'new' calls for the same purpose.
Btw, to delve more into why I used std::set in the example:
The point was to demonstrate that 'new' and 'delete' are heavy
operations. But how heavy? It's clear that if you do nothing else in
a program other than N allocations or 2*N allocations, the latter will
take about twice the time compared to the former. However, that tells
little about how heavy the allocation is compared to other operations
that the program might be doing. If 'new' and 'delete' take one clock
cycle then it's rather irrelevant.
std::set does quite a lot of things when elements are inserted. It's
a balanced binary tree, and every time an element is inserted, especially
if there is already a big amount of elements, it will perform quite many
operations, mainly to re-balance the tree. It will traverse the tree and
update pointers and flags, etc. (It does this to about O(log n) elements
of the tree, but we are still talking about dozens of operations needed
to re-balance the tree.)
So inserting an element to a std::set is a relatively heavy operation
because of all the operations it needs to do internally to re-balance the
tree. Thus one would easily think that when inserting 10 million elements
to a std::set, the vast majority of the time is being spent on these
operations.
However, rather surprisingly, over 75% of the time is actually being
used by 'new'. In other words, 'new' is at least three times slower than
re-balancing a binary tree with millions of elements in it.
This goes to demonstrate how heavy 'new' is, and why it may be a good
idea to minimize how many times it's called in a program, if possible.
If you want to directly measure the speed difference between the struct
hack allocation and the more regular solution, doing that is rather easy
with a program like:
#if(1)
//--------------------------------------------------------------
#include <cstdlib>
struct MyStruct
{
int someData;
int size;
int array[0];
};
int main()
{
for(int i = 0; i < 100000000; ++i)
{
const int arraySize = 10 + i % 10;
MyStruct* p = (MyStruct*) std::malloc
(sizeof(MyStruct) + arraySize * sizeof(int));
std::free(p);
}
}
//--------------------------------------------------------------
#else
//--------------------------------------------------------------
#include <vector>
struct MyStruct
{
int someData;
std::vector<int> array;
MyStruct(int size): array(size) {}
};
int main()
{
for(int i = 0; i < 100000000; ++i)
{
const int arraySize = 10 + i % 10;
MyStruct* p = new MyStruct(arraySize);
delete p;
}
}
//--------------------------------------------------------------
#endif
For example in my computer the first version of the program, which uses
the struct hack, takes 10 seconds to run, while the latter version takes
20 seconds to run.
Of course given the attitude that some people seem to have formed in
this thread, I'm pretty sure someone will come up with some reason why
I "cheated" there as well (most probably by ignoring the original premise
that the struct needs to be allocated dynamically in cases where it has
to outlive the scope where it's being created).