- Home /
Dictionary and List Efficiency Issues
So in the game I'm working on I have a dictionary with information of all the characters. The key is the name of the character, and each corresponding value has a type of Info
, which is a custom class I made myself containing a bunch of properties of that specific character.
In the class Info
, there're basically two types of properties: one is more like independent, something I need to hardcode like the character description - and I'm loading them from a config file; while the other is more like dependent, like you can calculate them when you use this instance of Info.
What I'm doing right now is that I'm constructing the dictionary inside Awake()
with only the second type of the properties. But in order to fill in the other information in the dictionary, I load everything I need from the config file as a List
, and then go through the list and push the missing info into the dictionary. With this O(n), I actually have to go through a dictionary first and then a list.. So I'm not sure if there's a better practice out there..
The other option I can think of is that I only construct the dictionary with info from config file first, and not calculating the other info until the character is in use, which is more like I calculate it at runtime.
I'm not sure whether I'm overthinking or not. I've been struggling with it for a night and have no idea like does it really matter and if so, which one is better?
Sorry for the lengthy question and any help would be appreciated here.
You're probably talking about milliseconds anyway. If this is behind a loading screen, it's unlikely to make much difference. Reading the file might even represent a greater time-cost than doing the extra work involved in your method. Unless you're doing sorts or other costly operations as you go, I would make it work in the most convenient, readable manner possible and slate it for refactoring in the "if I get around to it" pile.
This would be a performance problem on a 486 or lower computer. Don't bother with premature optimization. If it runs at above 30fps in the editor, everything is perfectly fine. Start thinking about whether or not it should run better and faster when the game jerks or freezes when certain scripts are executed.
Answer by sssss53 · Mar 20, 2015 at 12:35 PM
AlwaysSunny and Nymisu, thanks guys! I think you're right. I'll get it done first anyways. You just remind me of that famous
Never thought of myself doing the same evil when I actually come down to that point :') Thanks again!Premature optimization is the root of all evil.