Skip to main content
gamedev.net
🔒 Locked

Array VS Vector

Started by -Datriot- Aug 21, 2008 at 4:08 PM 10 replies 2.5k views
Original Post
-Datriot-
-Datriot-
Hi, I'm creating a card game for the DS, and my Deck class holds all the cards of that deck (naturally.) I was thinking with the DS being very limited in terms of performance, it might be a lot faster to have a static array over the dynamic vector.

#define DECK_SIZE 40

class Deck
{
    Card cards[DECK_SIZE];
    // Other deck stuff here
};


class Deck
{
    Vector<Card> cards;
    // Other deck stuff here
};

Would allocating memory for 40 cards when the deck is created have less of an impact on performance instead of allocating and freeing memory every time a card is added or removed?
Driv3MeFar
Driv3MeFar
Quote:
Original post by -Datriot-
allocating and freeing memory every time a card is added or removed?


Why would that happen? If you know the size of you deck before hand, you can reserve enough space in the vector to hold all your cards in a single allocation.
chairthrower
chairthrower
Quote:
allocating and freeing memory every time a card is added or removed


reserve() as suggested by Driv3MeFar -

in any case std::vector does *not* allocate memory everytime an element is added and removed.

alternatively boost::array will give you a familiar stl layer (iterator,algorithm etc) over raw stack.

just how slow is the DS ?
gardin
gardin
Well, if you we're programming in C, you could use realloc :D and get the benefit of an array :P But that is a bad idea in C++.. I would stick with Vector, it's easier to manage in the end..
frob
frob
Quote:
Original post by -Datriot-
it might be a lot faster to have a static array over the dynamic vector.

*** Source Snippet Removed ***

Would allocating memory for 40 cards when the deck is created have less of an impact on performance instead of allocating and freeing memory every time a card is added or removed?

You should only be allocating and releasing this memory on very rare occasions, only when loading and releasing all your assets.

This is not a performance concern at all. Memory access speeds are the same for vectors and arrays, since vectors are implemented as an array.

There is a very minor difference in space requirements (a few bytes of overhead for the vector) but in the Grand Scheme of Things, those few bytes will be inconsequential unless you have many thousand decks of cards at a time.
Zahlman
Zahlman
First off: use const identifiers for constants, not text substitution hacks (#define). And since when does a deck have only 40 cards?

But to answer your question:

The DS isn't limited in performance *enough* for this to matter, unless you're trying to, for example, determine your odds in hold-em poker with no community cards showing by brute force. Making a memory allocation is "slow", compared to, say, a multiplication; but it's still many times faster than reading something from a hard disk, and approximately a zillion times faster than waiting for the person using your program to make a yes-or-no decision.

But then, the vector does not reallocate "every time a card is added or removed", anyway. Every time it runs out of capacity, it will reallocate to increase its capacity by a constant factor (not amount). The net result is that you can square the size of your deck, and merely double the number of reallocations. And it will actually never reallocate for a card removal.

In fact, as a general principle for software engineers - a very important one: don't "worry" about things. Try it. If it works, it works. Full stop. If you can imagine it being such a big problem that you can't even try it, read up on it first. Later, if you determine that it is a problem, read up more to make sure you're using the tools properly ('tis a poor workman who blames his tools).
-Datriot-
-Datriot-
Thanks for the input guys, I guess I was working under a false assumption about the stl::vector. I do tend to worry about all these little nit-picky things, I'll continue using vector for all my deck-related needs. =p
And I'll try worrying potential peformance bottlenecks when they actually occur. ;)
Evil Steve
Evil Steve
Quote:
Original post by -Datriot-
I was thinking with the DS being very limited in terms of performance, it might be a lot faster to have a static array over the dynamic vector.
For something like a card game this isn't going to be an issue at all. We use the STL all over the place in our code at work (DS driving games), and it's never been a problem. As Zahlman said, this is where reserve() comes in.
nobodynews
nobodynews
std::vector::resize vs. std::vector::reserve

If it isn't clear from reserve, it specifies the minimum capacity. If the current capacity is greater than the number you supply to reserve(ie, capacity is 20, you reserve 10), it will (probably) do nothing. Not make the vector larger nor smaller. Resize will destroy extra objects. Neither is likely to shrink capacity, just the size.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
Black Knight
Black Knight
Meh reserve doesnt tell what happens when the vector holds more elements than the size that is trying to be reserved.
resize just adds or removes elements and "sizes" the vector.
Reserve needs a bit clarification me thinks ^^
nobodynews
nobodynews
SGI's documentation for std::vector. Their description of reserve is more clear and it agrees with what I wrote.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.