- Home /
How to store and iterate through a lot of Vector2s?
Hey,
I'm working on a random generator with pre-generating, so I don't really care about performance. However RAM usage goes really high with about 10k-20k of V2s, so what's the best way to store that amount of data for usage until everything is generated? I guess an array costs less RAM than a generic list (though is it a lot less?), but then are there any tricks like if you "translate" V2 to string, would it be visibly RAM cheaper with parsing?
And on side, what's the fastest way to iterate through that?
Edit: Thanks for additional information on the topic, I didn't really get into this stuff and when I hit a lot of RAM I was like, there is no infinite loop, no "100 times adding the same value to list" problem, so I thought that It may be that I'm using something not suitable for this etc. In the end I don't know what was wrong, just wrote the algorithm cleaner and it works for as many chunks as wanted.
Did you try translating your Vector2 to a string and performance test it next to a Vector2 array? That will answer all your questions. Just take 100 Vector2's and iterate through them in 2 different ways.
It would help if you post what you're currently doing to reach that amount!
Answer by Bunny83 · Apr 04, 2017 at 09:38 PM
I don't see what you mean by high RAM usage. Even when you have 100k * Vector2 it's just 800KB of memory. That's not even 1MB.
The overhead between a generic List and a native Array is neglectable small. It's a constant total overhead of about 16 bytes. A List, if not initialized with a known capacity might have some spare capacity to increase performance of future additions. However that's nothing you should care about.
A single Vector2 is composed of two float values. A single float value has a size of 4 byte. So a Vector2 has a size of 8 bytes. A string representation of a Vector2 would require (in almost all cases) the same or even more memory as our decimal system is very inefficient (10 different values per byte while binary can store 256 values per byte)..
Also there are several different goals when optimising a process / system. Either memory efficiency or speed efficiency. Those two are in most cases mutually exclusive. It's often possible to reduce memory overhead by increased performance load or to increase performance by additional memory overhead. The question is what kind of optimisation is required.
If you don't have a concrete problem yet any optimisations would be premature optimisations.
Bunny83 is correct. 20,000 Vector2's should be a rather insignificant memory footprint for any modern computer or even phone. 20,000 Vector2's should take up only 156.25 kbytes of memory. You must have something else eating up your RA$$anonymous$$ that you haven't considered.
Answer by AndreM1 · Apr 04, 2017 at 10:53 PM
I did manage to make some benchmarking, up to 2kk of vector2 on array, to keep them alive i set them as static an constantly changing some values, the garbage collector didn't seem to be much happy with it, but the size? 0.04 GB of ram, with 2.000.000 of vector2, you should really be more afraid of iterating over them wich proven to be kind of more slow. I also tested lists, wich proven to be the same thing
Your answer
Follow this Question
Related Questions
Unity Tile Array vs Int Array Performance 0 Answers
GC Allocation with new array length 0 Answers
Performance question - Loading large-ish amounts of text data in a mobile game? 1 Answer
Is it faster to access an Vector3 array than to access a mesh's vertices? 2 Answers
List/Array should handle another List/Array - Upgrade System - Performance 1 Answer