Re: Zero-size array as struct member
"Juha Nieminen" <nospam@thanks.invalid>
In C++ you should use vector<int> instead, it is about as fast.
In which system, exactly?
Unfortunately 'new' (and 'delete') is a rather heavy operation with
most C/C++ standard memory allocators, and if you use std::vector in
the place of the struct hack, you will be doing two 'news' instead of
one malloc().
Why? IMO, new and delete are, on the success path, practically
equivalent on many common implementations. Difference is one if on
malloc failure, and new is inlined.
You are basing your claims on your personal *opinion*? Rather than,
you know, actually testing it in practice?
I say this claim of yours is poorly founded.
It's quite well founded. For example, take this short piece of code:
int main()
{
std::set<int> someSet;
for(int i = 0; i < 10000000; ++i) someSet.insert(i);
}
Compiling that with "g++ -O3 -march=native" on my Pentium4 system takes
about 10 seconds. So, where do you think all that time is spent? Perhaps
rearranging the binary tree each time a new element is inserted? That
sounds
like something which would take its time.
Nope. The majority of that time is spent *allocating memory*.
Hard to decide that your article is a sour attemt at cheating or just basic
trolling. We discuss performance of vector<int> then you insert a snippet
with set<int> like it had any relevance. :-(((
Different collections have different characteristics wrt performance, memory
footprint, etc, we ALL know that. Now please go back to the original claim
instead of drifting in all directions.
....
If you are allocating a million instances of the struct, each such
instance having an std::vector object inside, reserve() would do nothing
to alleviate the memory fragmentation.
And if you allocate a million instances of the struct hack, it will take a
while too. now do you suggest using the struct hack in the *payload*?