Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是字典<;TKey,TValue>;比列表中的LINQ更快<;T>;?_C#_List_Collections_Dictionary - Fatal编程技术网

C# 是字典<;TKey,TValue>;比列表中的LINQ更快<;T>;?

C# 是字典<;TKey,TValue>;比列表中的LINQ更快<;T>;?,c#,list,collections,dictionary,C#,List,Collections,Dictionary,我通常使用List进行收藏。 但是,如果我需要快速查找集合,例如,在下面的示例中,我将使用字典,以便通过id快速查找集合: Dictionary<int, Customer> 字典 但是既然我可以使用LINQ来查询列表,如下所示,那么有没有理由麻烦使用字典而不是列表?是字典更快,还是LINQ在幕后做了一些让它同样快的事情 using System; using System.Collections.Generic; using System.Linq; using System.

我通常使用
List
进行收藏。 但是,如果我需要快速查找集合,例如,在下面的示例中,我将使用字典,以便通过
id
快速查找集合:

Dictionary<int, Customer>
字典
但是既然我可以使用LINQ来查询
列表
,如下所示,那么有没有理由麻烦使用字典而不是列表?
是字典更快,还是LINQ在幕后做了一些让它同样快的事情

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>()
            {
             new Customer { Id = 234, FirstName = "Jim", LastName = "Smith" },
             new Customer { Id = 345, FirstName = "John", LastName = "Thomas" },
             new Customer { Id = 654, FirstName = "Rick", LastName = "Ashton" },
             new Customer { Id = 948, FirstName = "Rod", LastName = "Anders" }
            };

            var customer = (from c in customers
                           where c.Id == 654 select c).SingleOrDefault();
            Console.WriteLine(customer.Display());

            Console.ReadLine();

        }
    }


    public class Customer
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        internal string Display()
        {
            return String.Format("{0}, {1} ({2})", LastName, FirstName, Id);
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列出客户=新列表()
{
新客户{Id=234,FirstName=“Jim”,LastName=“Smith”},
新客户{Id=345,FirstName=“John”,LastName=“Thomas”},
新客户{Id=654,FirstName=“Rick”,LastName=“Ashton”},
新客户{Id=948,FirstName=“Rod”,LastName=“Anders”}
};
var customer=(来自客户中的c)
其中c.Id==654选择c).SingleOrDefault();
Console.WriteLine(customer.Display());
Console.ReadLine();
}
}
公共类客户
{
公共int Id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
内部字符串显示()
{
返回String.Format(“{0},{1}({2})”,LastName,FirstName,Id);
}
}
}

对于小于20项的列表,字典/哈希表的开销将导致它比列表慢。

如果您从逻辑上想创建一个集合,以便您可以根据客户的ID轻松查找客户,我会使用某种形式的
IDictionary
。这表达了你想要达到的目标


现在,您可以使用列表来做同样的事情,正如leppie所说,对于小数据集,它的速度差不多,甚至更快——但是对于小数据集,它的速度无论如何都非常快,那么您为什么要在意呢?我认为更重要的是告诉你的代码读者你想用这个集合做什么——我认为,字典要比列表更有效地达到这个目的。

LINQ不是魔术。它仍然需要遍历列表才能找到所需的元素。字典将更快(如leppie所指出的,对于大小合适的集合)

根据基于键从字典中获取项“接近O(1)操作”。另一方面,在列表上执行循环遍历元素以查找匹配项。所以一般来说,字典肯定会更快


如果您想加速Linq操作,可以使用它在集合上放置索引。

您可能可以使用SortedList并在此集合上执行二进制搜索(考虑到在第一次比较后它会消除一半集合)

LINQ在这种操作中通常较慢。然而,在一个足够小的集合上(例如您的示例),由于开销的不同,它可能会更快。然而,在一个足够小的集合上(例如您的示例),两种解决方案之间的差异将非常小,以至于它与字典查找或Where()读取更自然的问题没有多大关系。

有趣。你在哪里找到那些号码的?我有兴趣阅读更多关于它的内容。这是从旧的HybridDictionary类中删除的吗?@XIII:吸拇指的猜测:)我发现这很有趣:(有趣的是注意到HybridDictionary只对很少的~5个条目更快——当然,使用了描述的特定设置)这篇文章真的是“demagics”linq。我发现它非常有用。:)