- Home /
Which to use Arrays, Collections, Lists and Generics
Hi all, firstly, please try to remember that the Unity team themselves use Collections :)
From a coding point of view this to me is the most important decision to make in how I code i.e. what to use for set processing. I know the opinion of some but I'd like the communities' thoughts, the reason is because I need to make a decision for how to direct a team of devs to do this on a commercial game.
I'd like some help on this, for the code listings below, the array version seems to be 1.5x faster than the List version but I personally believe that the List version is better and less coding, which should I use the array versions or the List versions to find / see if objects exist in a set and then work with them. I personally don't think the difference is any big deal compared to other performance issues like models, textures and lighting / shaders. The Generics version is a lot less code and more modern / recommended by the .NET team. Please let me know your recommendations.
Cheers.
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; using System; using System.Diagnostics;
public class DynamicMegaMod : MonoBehaviour {
private bool Exists(int val, MyClass[] search)
{
foreach (MyClass i in search)
if (i.MyValue == val)
return true;
return false;
}
private MyClass Find(int val, MyClass[] search)
{
foreach (MyClass i in search)
if (i.MyValue == val)
return i;
return null;
}
public class MyClass
{
public int MyValue { get; set; }
public string AnotherValue { get; set; }
public string AnotherValue2 { get; set; }
}
// Use this for initialization
void Start () {
System.Diagnostics.Stopwatch sw1 = new System.Diagnostics.Stopwatch();
//trying to find if an element exists in an array of 100000, 10000 times
sw1.Start();
MyClass[] myArray = new MyClass[100000];
for (int i = 0; i != 100000; i++)
myArray[i] = new MyClass() {MyValue=i};
for (int i = 0; i != 10000; i++)
{
if (Exists(50000, myArray))
{
}
}
sw1.Stop();
System.Diagnostics.Stopwatch sw2 = new System.Diagnostics.Stopwatch();
//trying to find if an element exists in a list of 100000, 10000 times
sw2.Start();
List<MyClass> myList = new List<MyClass>(100000);
for (int i = 0; i != 100000; i++)
myList.Add(new MyClass() {MyValue=i});
for (int i = 0; i != 10000; i++)
{
if (myList.Exists(mv => mv.MyValue == 50000))
{
}
}
sw2.Stop();
System.Diagnostics.Stopwatch sw3 = new System.Diagnostics.Stopwatch();
//trying to find an element in an array of 100000, 10000 times
sw3.Start();
MyClass[] myArray2 = new MyClass[100000];
for (int i = 0; i != 100000; i++)
myArray2[i] = new MyClass() {MyValue=i};
for (int i = 0; i != 10000; i++)
{
if (Find(50000, myArray2) != null)
{
}
}
sw3.Stop();
System.Diagnostics.Stopwatch sw4 = new System.Diagnostics.Stopwatch();
//trying to find an element in a list of 100000, 10000 times
sw4.Start();
List<MyClass> myList2 = new List<MyClass>(100000);
for (int i = 0; i != 100000; i++)
myList2.Add(new MyClass() {MyValue=i});
for (int i = 0; i != 10000; i++)
{
if (myList2.Find(mv => mv.MyValue == 50000) != null)
{
}
}
sw4.Stop();
}
// Update is called once per frame
void Update () {
/* do something */
}
}
I use List often, but when I need to pass something through Unity API (like in $$anonymous$$esh and ParticleEmitter) it must be an Array. Using .ToArray() usually leads to ugly GC.Colect spikes, at least in my experience.
I wouldnt divide the different options into "what is best". I would carefully select the right tool for the right job. Each option has its advantages, thats why they exists. Array are "low-level" and fast, but the help functions arent too advanced and they arent dynamic. List on the other hand, is quite easy to implement with logic and elegant code. But they are much slower because of these extra help functions.
If you go for speed only and dont care about the extra coding, then go for low-level Array[].
Your answer
![](https://koobas.hobune.stream/wayback/20220613062241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script Efficiency 1 Answer
Low Priority Methods In LateUpdate? 1 Answer
What is a reasonable number of draw calls relative to one hardware configuration ? 6 Answers
Should I export my room with all the objects in it or rearrange in Unity? 1 Answer
Which Implementation is Better? A Performance Question 1 Answer