C#基础之理解数组、 列表和字典的详细 、Stopwatch时间戳

目录

1.先上结论

2.测试代码

3.打印结果

4.结论


1.先上结论

每一种集合都有特定的用途,都有自己的优点和缺点。

本文主要依据遍历速度和搜索速度比较

  优点 缺点
数组 遍历速度快 长度固定,扩展费性能
列表

方便扩展

遍历速度比字典快

搜索速度比数组快

遍历速度比数组慢

搜索速度比字典慢

字典

方便扩展

搜索速度快

遍历速度慢

2.测试代码

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class GenericCollectionsTest : MonoBehaviour
{
    #region PUBLIC_DECLARATIONS  
    public int numberOfIterations = 10000000;
    #endregion
    #region PRIVATE_DECLARATIONS  
    private Stopwatch stopWatch;
    private List intList;                 // 整数列表  
    private Dictionary intDictionary;    // 一本字典,键和值为整数。  
    private int[] intArray;                     // 一个整数数组  
    #endregion
    #region UNITY_CALLBACKS  
    void Start()
    {
        stopWatch = new Stopwatch();
        intArray = new int[numberOfIterations];
        intList = new List();
        intDictionary = new Dictionary();
        AddFakeValuesInArray(numberOfIterations);
        AddFakeValuesInList(numberOfIterations);
        AddFakeValuesInDictionay(numberOfIterations);
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PerformTest();
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            SearchInList(111);
            SearchInDictionary(numberOfIterations - 1);
            UnityEngine.Debug.Log("SearchComplete");
        }
    }
    #endregion
    #region PRIVATE_METHODS  
    private void AddFakeValuesInArray(int iterations)
    {
        for (int i = 0; i < iterations; i++)
        {
            intArray[i] = Random.Range(0, 100);
        }
    }
    private void AddFakeValuesInList(int iterations)
    {
        for (int i = 0; i < iterations; i++)
        {
            intList.Add(Random.Range(0, 100));
        }
        intList[iterations - 1] = 111;
    }
    private void AddFakeValuesInDictionay(int iterations)
    {
        for (int i = 0; i < iterations; i++)
        {
            intDictionary.Add(i, Random.Range(0, 100));
        }
        intDictionary[iterations - 1] = 111;
    }
    private void SearchInList(int value)
    {
        #region FIND_IN_LIST  
        stopWatch.Start();
        int index = intList.FindIndex(item => item == value);
        stopWatch.Stop();
        UnityEngine.Debug.Log("Index " +index);
        UnityEngine.Debug.Log("Time Taken to Find in List " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
        #region CHECK_IF_CONTAINS_VALUE_IN_LIST  
        stopWatch.Start();
        bool containsValue = intList.Contains(value);
        stopWatch.Stop();
        UnityEngine.Debug.Log(containsValue);
        UnityEngine.Debug.Log("Time Taken To Check in List " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
    }
    private void SearchInDictionary(int key)
    {
        #region FIND_IN_DICTIONARY_USING_REQUIRED_KEY  
        stopWatch.Start();
        int value = intDictionary[key];
        stopWatch.Stop();
        UnityEngine.Debug.Log("Time Taken to Find in Dictionary   " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
        #region CHECK_IF_DICTIONARY_CONTAINS_VALUE  
        stopWatch.Start();
        bool containsKey = intDictionary.ContainsKey(key);
        stopWatch.Stop();
        UnityEngine.Debug.Log(containsKey);
        UnityEngine.Debug.Log("Time taken to check if it contains key in Dictionary" + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
    }
    private void PerformTest()
    {
        #region ARRAY_ITERATION        // 循环遍历数组  
        stopWatch.Start();
        for (int i = 0; i < intArray.Length; i++)
        {
        }
        stopWatch.Stop();
        UnityEngine.Debug.Log("Time Taken By Array " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
        #region LIST_ITERATION            //  循环遍历列表中使用简单的 for 循环  
        stopWatch.Start();
        for (int i = 0; i < intList.Count; i++)
        {
        }
        stopWatch.Stop();
        UnityEngine.Debug.Log("Time Taken By List " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
        #region LIST_ITERATION_BY_FOREACH_LOOP             //  遍历列表中通过使用 foreach 循环  
        stopWatch.Start();
        foreach (var item in intList)
        {
        }
        stopWatch.Stop();
        UnityEngine.Debug.Log("Time Taken By List Using foreach  " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
        #region DICTIONARY_ITERATIOn_LOOP                //  遍历字典  
        stopWatch.Start();
        foreach (var key in intDictionary.Keys)
        {
        }
        stopWatch.Stop();
        UnityEngine.Debug.Log("Time Taken By Dictionary " + stopWatch.ElapsedMilliseconds + " ms");
        stopWatch.Reset();
        #endregion
    }
    #endregion
}

3.打印结果

C#基础之理解数组、 列表和字典的详细 、Stopwatch时间戳_第1张图片

让我们来了解数据结构,我们应该使用 考虑的几例 ︰

案例 1) 对象的数量保持不变在整个游戏

       显然不会改变对象的数目, 现在在这里就不值得用作列表或字典。

       在这里数组的性能列表快2倍 !

案例 2) 对象的数量在游戏过程中不断变化

       显而易见的选择是列表。作为对象不停地变化,它是比字典更快.      如果对象池,列出了常用来管理池。

列表与词典进行比较几乎是8-10 倍快,如果您遍历使用foreach循环List 几乎才会 3 倍的时间比正常的循环,从而增加了使用foreach循环的一个更多缺点上面的示例所示。如果你想要看看其他缺点的foreach循环 。

难道我应该完全停止使用的词典吗?

字典的查找 索引可是强项:

SearchInList()方法的第一部分是来查找列表中传递给它的值,检查如果它实际上包含的值,并返回布尔值

 SeatchInDictionary()方法的第一部分是要根据传递给它,键的值和第二部分将检查如果该方法具有特定键或不存在,通过使用ContainsKey() 再次按键调用这些方法的。

在看Log的输出:

C#基础之理解数组、 列表和字典的详细 、Stopwatch时间戳_第2张图片

              所以从图片显而易见的结论是,字典搜索时间是几乎为零。 所以每当要不断寻找一些对象在整个游戏的情况下,明智的选择就是选择词典!

4.结论

            结论是简单的有三个基本准则 ︰

  1. 当对象数目仍然相同,也有是没有要求的密集搜索不使用列表。
  2. 如果对象是动态和搜索不是一个优先事项,列表!
  3. 快速访问和对象,那么词典将是明智的选择

你可能感兴趣的:(#,C#基础)