Re: vector question
On Feb 4, 10:34 pm, NL <norv...@gmail.com> wrote:
Hi
First of all, thanks for the replies:
Regarding premature optimization, the creation and filling of the
vector is currently 90% of the execution time in the semi real time
system, so I'm just exploring what overhead I'm adding before jumping
in with both feet.
That's more precise, but still not enough for e.g. me to give more
precise answer. These 90%, that's 90% of what, of your target time for
"filling the vector"? Also, is that 90% of your current solution?
If answer to both is "yes", perhaps you have a lot of copying going
on? If so, can you work on avoiding that instead? (I am asking this
because one of best ways to get better performance __anywhere__ is to
avoid copying).
Here's what I also wonder: is your data max set size known at compile-
time? If so, are you happy with memory usage? If so, there is no point
in using a vector at all.
If the answer to any of these two questions is "no", then you
__should__ consider using a vector.
Given that you have numFoo, number of elements is not fixed. Also, at
least from the code shown, you don't ever remove them, and you can't
remove stuff from the middle using what you've shown. So, how does
"filling" look like? If you have a big fill somewhere at ramp-up, then
having empty Foo() and using resize() instead of a push_back loop
would give you pretty much same speed (difference is one trip to
heap).
If, on the other hand, GetNextFoo() happens occasionally while
running, and you do want to minimize heap pressure, it's difficult to
beat simple push_back. If you don't care much about heap usage, then
reserve might give you better more speed.
At any rate, all these questions are best answered by you, because you
know your situation best, and you are not explaining it in great
detail here, either. Also, STL containers have rather nicely defined
performance characteristics, that should be your guide for these
things.
You could just use a vector of pointers to Foo, or even better a
vector of smart pointers to foo so you don't get any issues with
memory allocation/deallocation.
I don't understand. What container are the actual Foo's stored in? If
a vector, then my issue still exists. (My issue is in creating and
filling the container of Foo objects. With the array, I currently fill
in an already existing chunk of memory,
To achieve similar with vector, use reserve().
with a vector I'll need to do
the same, but then copy the memory to another location.)
If that's your question, use empty Foo constructor and resize(), e.g.
struct Foo
{
Foo() {}
}
typedef std::vector<Foo> Foos;
....
Foos foos(some_initial_size); // resize() is here.
for (Foos::iterator p=foos.begin(); p!=foos.end(); ++p)
{
Foo& foo = *p;
foo.x = blah;
foo.y = blahblah;
//...
}
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]