unity C#字典增删改查并记录时间

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Dictionary : MonoBehaviour
{ Dictionary Dic = new Dictionary();
    public String Str;
    public int del;
  
    // Start is called before the first frame update
    void Start()
    {
        UnityEngine.Debug.Log("初始Dictionary");
        
        Dic.Add(10, "J");  
        Dic.Add(7, "G");  
        Dic.Add(2, "B");
        Dic.Add(1, "A");   
        Dic.Add(4, "D"); 
        Dic.Add(5, "E");
        Dic.Add(8, "H");
        Dic.Add(6, "F");
        Dic.Add(9, "I");
        Dic.Add(3, "C");
      

        foreach (KeyValuePair item in Dic)
        {
            UnityEngine.Debug.Log(item.Key + "      " + item.Value);
        }


        FindValue();
        Order();
        Delete();
        Insert();
        Clear();
    }

    private void FindValue()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        foreach(var key in Dic.Keys)
        {
            if (Dic[key]== Str)
                UnityEngine.Debug.Log(key);
        }
     
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Dictionary查找的时间为:" + ts);
    }

    private void Order()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        var dicSort = from objDic in Dic orderby objDic.Value ascending select objDic;//ascending升序,descending降序
        foreach (KeyValuePair kvp in dicSort)
        {
            UnityEngine.Debug.Log(kvp.Key + ":" + kvp.Value);
        }
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Dictionary排序的时间为:" + ts);
    }

   

    private void Delete()
    {
        var dicSort = from objDic in Dic orderby objDic.Value ascending select objDic;
        Stopwatch sw = new Stopwatch();
        sw.Start();
        Dic.Remove(del);
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        
        foreach (KeyValuePair kvp in dicSort)
        {
            UnityEngine.Debug.Log(kvp.Key + "      " + kvp.Value);
        }
       UnityEngine.Debug.Log("Dictionary删除的时间为:" + ts);
    }

    private void Insert()
    {
        var dicSort = from objDic in Dic orderby objDic.Value ascending select objDic;
        Stopwatch sw = new Stopwatch();
        sw.Start();
        Dic.Add(11, "K");
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
       
        foreach (KeyValuePair kvp in dicSort)
        {
            UnityEngine.Debug.Log(kvp.Key + "      " + kvp.Value);
        }
 UnityEngine.Debug.Log("Dictionary插入的时间为:" + ts);
    }

 private void Clear()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        Dictionary Dic = new Dictionary();
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Dictionary重置的时间为:" + ts);

    }


    // Update is called once per frame
    void Update()
    {
        
    }
}


 

你可能感兴趣的:(unity,unity,c#)