Reputation: 1837
I realise the question isn't very clear so I will expand what I mean (if anyone can think of a better way to phrase it then that would be greatly appreciated).
I have a particular class type (Attribute) that any instance of another class (Character) will need several of (e.g. if there are Strength, Intelligence and Luck in the game then each Character will need 3 instances of the Attribute class).
The information for each of the Attributes is stored in AttributesList.txt.
Is it better to read this file once at the beginning of the program to create a global const vector<Attributes>
and have each Character create a copy of the vector of for their own use for as long as they exist, or to read the file every time a Character is created to create their Attributes?
Note: creating one instance of each Attribute and using pointers to reference it where needed isn't an option since each Character's Attributes have different values.
Upvotes: 0
Views: 87
Reputation: 57708
The question should be, "how often do I update or access the attributes?"
In many games (such as Wii and Playstation), they update the memory version of the attributes, then provide a command for saving the attributes to a file.
If the attributes do not occupy a gigantic amount of memory, then keep them in memory. This will speed up access times whenever your program is accessing them. Anything external to your program will take more time to fetch.
Remember, profile before optimizing. Also, get the program working correctly and robust before considering profiling.
Upvotes: 1
Reputation: 104539
This is one of those situations where you should evaluate the trade off between "what's easier to code?" vs "will it really make a difference?".
One one hand, since I/O is typically "slow" (compared to memory accesses) and a source of errors/exceptions, reading once when your game starts is not a bad idea.
However, if it's easier to code and there is little risk to the AttributesList.txt file getting corrupted, and it doesn't generate any measurable performance issues, re-reading the file each time an instance of your character class might work as well. How often does a new character get created?
When I worked in a game studio, we had compiled object resources saved (serialized) on disk. Each time a new game object was created, we'd re-read the struct from file. I think we did this as an async task off the main game/render thread so it wouldn't impact performance.
Upvotes: 2
Reputation: 1854
The answer to your question highly depends on your code. How large is this vector? How often do you need to get a copy of it? How large is the memory used by your application in general?
For example, if the size of this vector is a few percents of your total memory footprint and you copy it frequently then I will choose speed over memory footprint optimization (getting a copy of an object in memory is much faster than all the IO overhead you would need to read the get it from disk).
While intuition can help you decide with which to go. The definitive answer is by testing each case and profiling.
Upvotes: 2