「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

【UnityC#】Array.IndexOf、List.IndexOfの速度比較

Array.IndexOf、List.IndexOfの速度比較をしたので結果を残しておきます。

[ 環境 ]

Unity 2021.2.19.f1

[ 比較結果 ]

Array.IndexOf 12702 ms
List.IndexOf 12864 ms

[ スクリプト ]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class PerformanceComparison : MonoBehaviour
{
    void Start()
    {
        System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();

        int Count = 1000;

        int Target = Count - 1;

        int[] Array_ = new int[Count];
        List<int> List_ = new List<int>(){};

        for(int i = 0; i < Count; i++)
        {
            Array_[i] = i;
            List_.Add(i);
        }

        //Array.IndexOf

        StopWatch.Restart();

        for(int i = 0; i < Count; i++)
        {
            int Temporary = Array.IndexOf(Array_, Target);
        }

        Debug.Log(StopWatch.ElapsedMilliseconds);

        //List.IndexOf

        StopWatch.Restart();

        for(int i = 0; i < Count; i++)
        {
            int Temporary = List_.IndexOf(Target);
        }

        Debug.Log(StopWatch.ElapsedMilliseconds);
    }
}

[ 関連記事 ]