LoginSignup
3
1

More than 5 years have passed since last update.

Unity Loopの速度比較

Posted at

検証コード

LoopTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoopTest : MonoBehaviour 
{
    static readonly int TEST_COUNT = 10000;
    static readonly int ARRAY_NUM = 1000;
    int[] _LoopArray = new int[ARRAY_NUM];

    List<int> _LoopList = new List<int>();

    // Use this for initialization
    void Start () 
    {
        for (int i = 0; i < ARRAY_NUM; i++) {
            _LoopArray [i] = i;

            _LoopList.Add (i);
        }
    }

    // Update is called once per frame
    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.A)) 
        {
            Test1 ();
        }

        if(Input.GetKeyDown(KeyCode.S)) 
        {
            Test2 ();
        }
    }

    void Test1()
    {
        var start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            for (int i = 0; i < ARRAY_NUM; i++) {
                var a = _LoopArray [i];
            }
        }

        var lap1 = Time.realtimeSinceStartup - start;

        start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            for (int i = 0; i < _LoopArray.Length; i++) {
                var a = _LoopArray [i];
            }
        }

        var lap2 = Time.realtimeSinceStartup - start;


        start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            foreach(var value in _LoopArray){
                var a = value;
            }
        }

        var lap3 = Time.realtimeSinceStartup - start;

        Debug.Log (lap1);    //0.08274078sec
        Debug.Log (lap2);    //0.06918979sec
        Debug.Log (lap3);    //0.07750511sec
    }

    void Test2()
    {
        var start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            for (int i = 0; i < ARRAY_NUM; i++) {
                var a = _LoopList [i];
            }
        }

        var lap1 = Time.realtimeSinceStartup - start;

        start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            for (int i = 0; i < _LoopList.Count; i++) {
                var a = _LoopList [i];
            }
        }

        var lap2 = Time.realtimeSinceStartup - start;


        start = Time.realtimeSinceStartup;

        for (int test = 0; test < TEST_COUNT; test++) {
            foreach(var value in _LoopList){
                var a = value;
            }
        }

        var lap3 = Time.realtimeSinceStartup - start;


        Debug.Log (lap1);    //0.1496518sec
        Debug.Log (lap2);    //0.2155538sec
        Debug.Log (lap3);    //0.3233421sec
    }
}

検証結果

通常の配列 List
要素数で回す 0.08274078sec 0.1496518sec
length,countで回す 0.06918979sec 0.2155538sec
foreach 0.07750511sec 0.3233421sec

結論

要素数が固定なら配列を使ったほうが速い
配列は固定の変数よりlengthを使ったほうが速い

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1