详解C# List<T>的Contains,Exists,Any,Where性能对比

测试

新建一个Person

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

初始化List 中有一百万条数据,然后分别通过每种方法判断xiaoming是否在List中,代码如下

static void Main(string[] args)
    {
      List<Person> persons = new List<Person>();
      //初始化persons数据
      for (int i = 0; i < 1000000; i++)
      {
        Person person = new Person("My" + i,i);
        persons.Add(person);
      }
      Person xiaoming=new Person("My999999", 999999);
       
      //下面通过三种方法判断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耗时:" + watch.Elapsed.TotalMilliseconds);
      Console.WriteLine("Exists耗时:" + watch1.Elapsed.TotalMilliseconds);
      Console.WriteLine("Where耗时:" + watch2.Elapsed.TotalMilliseconds);
      Console.WriteLine("Any耗时:" + watch3.Elapsed.TotalMilliseconds);
      Console.ReadLine();
    }

执行结果如下图所示

结论

通过上图可以看出性能排序为

Contains > Exists > Where > Any

注意:
Contains中不能带查询条件


LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

1. Any

返回没有ProductCategory

var expr = from c in context.Categories
            where !c.Products.Any()
            select c;
SELECT 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[CategoryName] AS [CategoryName]
    FROM [dbo].[Category] AS [Extent1]
    WHERE  NOT EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Product] AS [Extent2]
        WHERE [Extent1].[CategoryID] = [Extent2].[CategoryID]
    )
var expr = from c in context.Categories
            where !c.Products.Any(p => p.UnitPrice > 10m)
            select c;
SELECT 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[CategoryName] AS [CategoryName]
    FROM [dbo].[Category] AS [Extent1]
    WHERE  NOT EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Product] AS [Extent2]
        WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[UnitPrice] > cast(10 as decimal(18)))
    )

2. All

var expr = from c in context.Categories
           where c.Products.All(p => p.Discontinued)
           select c;
SELECT 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[CategoryName] AS [CategoryName]
    FROM [dbo].[Category] AS [Extent1]
    WHERE  NOT EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Product] AS [Extent2]
        WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[Discontinued] <> cast(1 as bit))
    )

3. Contains

var expr = from p in context.Products
            where new string[] 
            { 
                "LINQ to Object",
                "LINQ to ADO.NET", 
                "LINQ to XML" 
            }
            .Contains(p.ProductName)
            select p;
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent1].[UnitPrice] AS [UnitPrice], 
    [Extent1].[UnitsInStock] AS [UnitsInStock], 
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Product] AS [Extent1]
    WHERE [Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML')
var expr = from p in context.Products
            where !(new string[] 
            { 
                "LINQ to Object",
                "LINQ to ADO.NET", 
                "LINQ to XML" 
            })
            .Contains(p.ProductName)
            select p;
SELECT 
    [Extent1].[ProductID] AS [ProductID], 
    [Extent1].[CategoryID] AS [CategoryID], 
    [Extent1].[ProductName] AS [ProductName], 
    [Extent1].[UnitPrice] AS [UnitPrice], 
    [Extent1].[UnitsInStock] AS [UnitsInStock], 
    [Extent1].[Discontinued] AS [Discontinued]
    FROM [dbo].[Product] AS [Extent1]
    WHERE  NOT ([Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML'))

C#中Linq的JoinWhere(m=>arr..Contains(m.id))的效率更高

开发工程中,经常遇到在集合中,根据另一个集合取出一些数据。

我之前常用的做法如下:

List<MyInfo> list = GetList(); // 约2W数据

int[] idArr = GetIDs();// 约几百

之前也一直没有去考虑效率问题。使用如下:

list.Where(m => idArr.Contains(m.id)).ToList();

通过条件,判断集合对象的id在数组中,逻辑没有问题,很正常的思维。(我的测试耗时约50ms)

但是,如果采用Join就会的到成倍的速度提升,代码如下:

list.Join(idArr, m => m.id, n => n, (m, n) => m).ToList();

我们换个思路,将idArr看做一个连接对象,用inner join方式连接,发现耗时缩短(我的测试与5ms)

由此可见:以上的使用情况,C#中Linq的JoinWhere(m=>arr..Contains(m.id))的效率更高。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值