亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解C# List<T>的Contains,Exists,Any,Where性能對(duì)比

 更新時(shí)間:2020年12月11日 08:43:48   作者:背過(guò)手  
這篇文章主要介紹了詳解C# List<T>的Contains,Exists,Any,Where性能對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

測(cè)試

新建一個(gè)Person類(lèi)

public class Person
  {
    public Person(string name,int id)
    {
      Name = name;
      Id = id;
    }
    public string Name { get; set; }
    public int Id { get; set; }

  }

初始化List 中有一百萬(wàn)條數(shù)據(jù),然后分別通過(guò)每種方法判斷xiaoming是否在List中,代碼如下

static void Main(string[] args)
    {
      List<Person> persons = new List<Person>();
      //初始化persons數(shù)據(jù)
      for (int i = 0; i < 1000000; i++)
      {
        Person person = new Person("My" + i,i);
        persons.Add(person);
      }
      Person xiaoming=new Person("My999999", 999999);
      
      //下面通過(guò)三種方法判斷persons中是否包含xiaoming
      Stopwatch watch = new Stopwatch();
      watch.Start();
      bool a = persons.Contains(xiaoming);
      watch.Stop();

      Stopwatch watch1 = new Stopwatch();
      watch1.Start();
      bool b = persons.Exists(x=>x.Id==xiaoming.Id);
      watch1.Stop();

      Stopwatch watch2 = new Stopwatch();
      watch2.Start();
      bool c = persons.Where(x=>x.Id==xiaoming.Id).Any();
      watch2.Stop();

      Stopwatch watch3 = new Stopwatch();
      watch3.Start();
      bool d = persons.Any(x => x.Id == xiaoming.Id);
      watch3.Stop();

      Console.WriteLine("Contains耗時(shí):" + watch.Elapsed.TotalMilliseconds);
      Console.WriteLine("Exists耗時(shí):" + watch1.Elapsed.TotalMilliseconds);
      Console.WriteLine("Where耗時(shí):" + watch2.Elapsed.TotalMilliseconds);
      Console.WriteLine("Any耗時(shí):" + watch3.Elapsed.TotalMilliseconds);
      Console.ReadLine();
    }

執(zhí)行結(jié)果如下圖所示

結(jié)論

通過(guò)上圖可以看出性能排序?yàn)?br />

Contains > Exists > Where > Any

注意:
Contains中不能帶查詢(xún)條件

到此這篇關(guān)于詳解C# List<T>的Contains,Exists,Any,Where性能對(duì)比的文章就介紹到這了,更多相關(guān)C# Contains,Exists,Any,Where內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論