Joining arrays

Hi, I'm using Unity but I don't think it's important. I have a list of object, on which I want to call a method which returns a regular array (struct[]) which can be quite big. Then I want to join all those arrays into a singular array. That big array will be generated only once, not modified and used for grabbing a random entry from that array multiple times. As I'm not that experienced in optimizing c#: 1. Is there any advantage in using array here instead of List<T> which I feel is easier to work with? 2. What's the best way to join those arrays into singular one? So far I'm doing it like that, but I think it is not optimal as I create new array for each iteration?
No description
6 Replies
oke
oke9mo ago
The performance difference in using a List<> instead of an array is negligible, especially with the overhead of an entire game engine in the background. However, if you plan on just keeping a selection of objects but not adding/removing to it regularly, it's better to use a regular array. What you have right now seems fine. Maybe use LINQ if you want more optimization.
magikusgierus
magikusgierusOP9mo ago
I feel time performance is not as important as memory performance, I fear Garbage Collector stepping in to free those arrays and dipping one frame. But I guess it will happen only once anyway
mtreit
mtreit9mo ago
If this is at process startup you don't really need to optimize it. A small point: you should use Array.Empty<T> instead of zero length arrays.
magikusgierus
magikusgierusOP9mo ago
Okay, I had to do some operations on those structs in the end I used a List and did nested foreach with .Add() Not entirely, even if I do it at the start (and not when loading level for example), the next GC cycle may happen in 20 secends, so 20 seconds after starting the game the player will get a dip
mtreit
mtreit9mo ago
Ephemeral GCs should clean it up much faster than that and I don't think it will show up at all.
oke
oke9mo ago
Optimization isn't needed at this point. Unless you're dealing with a large amount of elements in the list. Based on what we see right now, it's perfectly fine.
Want results from more Discord servers?
Add your server