Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by markl · Dec 20, 2011 at 11:25 PM · performance.netbestpractices

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 */
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image luizgpa · Dec 21, 2011 at 05:59 PM 1
Share

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.

avatar image BerggreenDK · Dec 21, 2011 at 06:44 PM 1
Share

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[].

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges