I have something like this :
I know I can do it with objects but I would like the fastest way as I don’t need to store the objects, just to fast access the values. I was thinking of Dictionnaries but it seems difficult.
Thanks !
This depends very much on exactly how you will be accessing them. A likely fastest way is have an array of units, where each unit is a struct.
Then a barracks is not defined by a list of units, but a list of integers that index into the unit array.
Why is this fast? Because the units end up in contiguous memory, which reduce GC overhead, and makes for super fast iteration since you leverage the cpu caches well.
But, if say, during a game loop, you access workers very often, but tanks very rarely, then it may make more sense to organize things differently, so that you aren’t iterating through tank memory when you don’t need it. So you might make tank a class so the array just stores the single reference rather than 3 values. Or store tanks in their own arrays, etc.
There are more exotic things you could do as well, like (int int double), let’s say that refers to X,Y,HEALTH. You could have an array of X, an array of Y and an array of health, all in a Units class. Why is this good? Well if you iterate to update positions of units, you can iterate over just that memory! And maybe vectorize the math too for a huge speedup.
And of course youll need to test, when experimenting, to see what really is working for you, I suggest Benchmarkdotnet, or if that isn’t workable, be sure to keep track of “milliseconds per frame” rather than “frames per second”. If this is a turn based game, then probably don’t worry about any of this unless its on some massive scale with billions of units.
Ok, i think I’ll go withEnum indexed { Worker, Gunner, Tank, Barracks, }
Static class units { Static Dictionary<indexed, int> x, Static Dictionary<indexed, int> y, ... }
That looks good to you performance wise ?
Dictionaries makes accessing “random” stuff fast. “random” meaning “not sequential” and it does so by creating a sort of index, a lookup of something unique to each object, into a list of values so that you don’t have to go over each one to find the one you are looking for. The benefits of which are apparent when you have a list of thousands of items.
I don’t know what you mean by “you just need fast access”, so you have to elaborate. What is it that is slow, that you need to have “fast” access. As u/AVX-512 said, it depends on how you access. How exactly are you accessing, and what are you doing with the values? Some code would be helpful.
You say you don’t want to use objects, does that mean you view accessing values through object properties slow? Perhaps you are prematurely optimizing. Your situation probably doesn’t need the level of optimization that u/AVX-512 is suggesting (though they are very insightful)
Dictionaries makes accessing “random” stuff fast. “random” meaning “not sequential” and it does so by creating a sort of index, a lookup of something unique to each object, into a list of values so that you don’t have to go over each one to find the one you are looking for. The benefits of which are apparent when you have a list of thousands of items.
Thanks for fixing my misconception 🙂
I don’t know what you mean by “you just need fast access”, so you have to elaborate. What is it that is slow, that you need to have “fast” access. As u/AVX-512 said, it depends on how you access. How exactly are you accessing, and what are you doing with the values? Some code would be helpful.
I want to operate on the simulated game state values, like for example, I want to produce a Gunner, I operate bank – cost of gunner.
You say you don’t want to use objects, does that mean you view accessing values through object properties slow? Perhaps you are prematurely optimizing. Your situation probably doesn’t need the level of optimization that u/AVX-512 is suggesting (though they are very insightful)
I indeed don’t have proofs that other way is too slow but I want the best performances (knowing that I don’t need OOP). Maybe I should have gone C++ :o) but it’s UWP app so I went standard.
Premature optimization is the root of all evil. Do you know you need this performance, or do you only believe you need it? If the latter, you need to benchmark to be able to say whether or not you truly do need it.
Sorry for basically saying the same thing you already heard a couple of times, but as a game programming teacher I have to agree. There are two types of performance: runtime performance of the application and production performance of the developers.
It’s a very popular mistake to only care about the former and ignore the latter. But the thing is I bet you that for every game that was not successful because of bad runtime performance there are at least ten games that were never released because the team didn’t manage to create something that is fun before funding ran out.
Optimize for development speed first. Once you have a game that is promising it will be relatively easy to improve the performance. Trying to improve the gameplay in a game that is optimized for runtime performance is usually much more difficult.
I’m not making a game 😛
It’s sort of AI but not really for a game.
I’m not sure what problem you’re having. Seems like they all inherit Unit, so why not just have a list of those?
Look into HashSet<T>, they are incredibly fast, and don’t allow duplicates
Arrays of structs. You can loop over them if all elements needs processing or individually with constant access time if needed.
What’s the advantage of not using objects? Why does everything have to be so fast and if so why C#? A lot of those abstractions are already pretty darn preformant. Often the over head is memory not speed to a degree. Objects are just a collection of pointers to functions under the hood so there’s some dereferencing but what are you doing where you can’t afford that hit.