Unity 3 dはforeachの使用をできるだけ避けるべきである

864 ワード

//       ,    GC Alloc,       unity           ,       :

using UnityEngine; 
using System.Collections;

public class ForeachTest : MonoBehaviour {

    protected ArrayList m_array;

    void Start () 
    { 
        m_array = new ArrayList(); 
        for (int i = 0; i < 2; i++) 
            m_array.Add(i); 
    } 
    
    void Update () 
    { 
        for (int i = 0; i < 1000; i++) 
        { 
            foreach (int e in m_array) 
            { 
                //big gc alloc!!! do not use this code! 
            } 
        }

        for (int i = 0; i < 1000; i++) 
        { 
            for (int k = 0; k < m_array.Count; k++) 
            { 
                //no gc alloc!! 
            } 
        } 
    } 
}

          2.3K GC Alloc(            GC。。。)。

          GC Alloc。

  ,     ,     foreach  。