.Net List vs builtin array performance

I had gotten the impression that one shouldn’t use .Net List unless you really needed to have its functionality (in Unity).
After reading up a bit on C# I found out that apparently .Net List is quite fast, only a bit slower that normal (builtin?) arrays.

So basically, how does .Net List compare to a builtin array in terms of performance?

A Linked List is very fast in most cases, however it can be a lot slower than a traditional array in some cases as well… Which you use depends entirely on your needs.

.NET (and Mono) support both a generic List and a LinkedList. The OP was asking about the former, not the latter. Generally speaking, Lists are quite fast, though I can’t say for sure how they compare to the in-built array.

Jeff

I don’t have any specific numbers to show you (a quick google for “c# list vs array performance” will yield plenty of test results though). However, at the end of the day your usage of one over the other is not going to impact performance in a meaningful way - just use what is best for the situation.

Is it a fixed number of elements? Use an array, no real reason not to. Otherwise, use a List<> - there’s really no reason not to there either. Personally, I default to using List<> - the only time I use arrays are when I know:

  1. It is a fixed size of elements that I’ll be working with
  2. I will be iterating over this list/array in a very, very tight loop.

If I answer yes to both of those questions, I use an array. Otherwise I use a List<> and move on with my day knowing that it really, really isn’t going to impact the performance of my project. Sometimes if I have conditions that change infrequently but need to be iterated over regularly, and memory usage isn’t a concern (really, it isn’t most of the time from the programming side) I’ll maintain a List<> - and when I update that list convert it to an array for internal usage (especially useful in something like, say, a flocking algorithm with a dynamic number of birds).

In addition to what KyleStaves said, it also depends on the type. The simpler the type, the faster a built-in array is compared to List. If you’re using an array of integers, it may be worth jumping through some hoops, depending on what you’re doing, to use int[ ] rather than List., since a built-in array is 5-6 times faster in that case. On the other hand, GameObject[ ] is barely any faster than List., so which one you’d use depends purely on what’s more convenient, and performance wouldn’t enter into it at all. As always, when it comes to “what’s faster”, remember that it’s easy to do some simple benchmarks, so you can quickly see for yourself instead of having to rely on hearsay (and if you have Pro, you also have the profiler).

–Eric

Some years ago I measured the time for creating and accessing to big arrays and lists. Lists were much faster than normal arrays in that test.

Lists in .Net (not sure about Mono, but I assume its equal) are basically simple Arrays the List class is only able to reallocate them when needed (Thats why a List contains Capacity beneath Count and thats why you can see in debug mode many null entries inside the items container of the List).

The usage of List only applies some overhead through the abstraction layer provided by the class self.

Whatever, why do you even need to consider the usage of Lists against the usage of builtin fixed Arrays? Both have well definied fields where to use them, like a firetruck against a police car.

That surprises me.

Anyhow, thanks to you all. Now I know there is no reason for me to create intricate functions for resizing builtin arrays because I worry about performance!

As everyone above me has said, it depends on what you’re using them for.

But, unless you are doing something very specific, I really doubt you will have to worry too much. By that I mean that there are hundreds of things that would be of greater significance to performance than List<> vs Array.

I only say this because a year ago I was developing this Bezier curve thing for Unity. It stores a whole bunch of Vector3 and floats in a ‘list’ and does some pretty intensive work with them each gameloop. And I initially kept in mind that Arrays were quicker and used them, then I just thought I’d test it out and switched to List<> and it made virtually no difference. There were so many other things that were more important to performance. Plus .Net or Mono or whatever can do some amazingly complex stuff with Arrays/lists before you’ll see any kind of performance decrease. That is, say, compared to creating gameobjects, or showing a GUI or whatever.

So, my advice, use List<>'s because they are easier, and you’ll probably never have to worry about optimising it anyway.

2c

I’d also say that it depends. I just finished testing out a particle system using two maxed-out ParticleEmitter objects. With 32,500 particles total, using a List got me 22-23fps on average, using a built-in array gets me around 33-34fps. I’m doing quite a few things every frame, including changing the position of every particle and assigning back to the ParticleEmitters’ particles.

I should say that, just like the Unity documentation suggests, performance improved by storing .position locally and assigning it back after calculation at least as much percentage-wise as List vs. built-in array. So those types of things are probably more important to focus on first.