

Same deal but you need occasional insertion and removal? List is the way to go. Understanding how each structure stores data in memory, how items are added / removed / accessed, and how well (or poorly) that matches up with your usage pattern will obliterate your need to benchmark this stuff in the vast majority of cases.*įixed number of items and you need it iterate the whole collection regularly? Array is a good fit.

In short, different data structures have strengths and weaknesses which lend themselves to different situations really well. This is relatively foundational knowledge that any good programmer should pick up relatively early on. Posters here have already mentioned learning about data structures and the pros and cons of each. Perhaps to do with string concatination as I'm curious about that one.Ĭlick to expand.You're right that it's highly situational, but you don't need to do benchmarks in most cases to get a solid idea of which is best. Gonna be posting some more benchmark tests on here soon guys. A single array vs list wont make a big different but if you have 100+ throughout the codebase, all of which are looping over 1000+ elements, it might be good to look into this during an optimisation phase. Micro optimisations won't make a big difference but if you find that an array works better for your needs, then go ahead with that, and perhaps manually call GC.Collect (i think thats the command) at a natural break point in the game. A colleague of mine switched from a list to an array when looping over a large data set and the different was literally a minute + faster.įYI, I did another benchmark only using 10 elements in the array / list, and using. I'd encourage people do their own benchmark in their projects and see what the difference is. I think when it comes to using a list vs array vs dictionary etc, it really depends on the situation. Hey guys, thanks for all the replies and input. That's good! Just make sure you're looking at the whole impact.

How often is the operation in question performed? How many items are in the collection? Unless it happens a lot, or the data set is large, or you've profiled to find the presence of an actual performance issue then considerations like this are likely to be premature optimisation.Įdit: That said, I don't want to appear condemning of curiosity. If it's not performance critical then, as long as it's not stupid, I'd take more stable over slightly faster.Īlso consider "faster" in context. If it's performance critical I'd use a higher performance collection in the first place, even if that means more work.

I personally wouldn't want to introduce unpredictable slowdowns into my code. I haven't checked out how well the new GC in Unity performs, but another consideration here is this: iteration time over your collection is probably fairly predictable, where the GC spikes you could cause are not. The average case may be a win in terms of specifically what you're measuring, but as others have pointed out the GC cost of allocating new arrays is going to kick you later. Click to expand.I think this is a classic case of why doing performance tests in synthetic tests like this can be misleading.
