是字典吗?比 List上的 LINQ 更快?
我通常使用 List
进行集合。 但是,如果我需要快速查找集合,那么例如在下面的示例中,我将使用字典,这样我就可以通过 id
快速查找它:
Dictionary<int, Customer>
但是因为我可以使用 LINQ 来查询List
无论如何,如下所示,是否有任何理由要经历使用字典而不是列表的麻烦? 字典更快还是 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);
}
}
}
I generally use List<T>
for collections.
But if I need a fast lookup on a collection, then e.g. in the following example I would use a Dictionary so I could look it up quickly by id
:
Dictionary<int, Customer>
But since I can use LINQ to query the List<T>
anyway, as below, is there any reason to go through the trouble of using a Dictionary instead of a List? Is Dictionary faster or is LINQ doing something behind the scenes that makes it just as fast?
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您逻辑上希望创建一个可以通过 ID 轻松查找客户的集合,我会使用某种形式的
IDictionary
。这表达了您想要实现的目标。现在你可以使用列表来做同样的事情,正如leppie所说,对于小型数据集,它会同样快甚至更快 - 但对于小型数据集,它无论如何都会很快,所以为什么你关心吗?我认为更重要的是告诉你的代码的读者你想用这个集合做什么——在我看来,字典比列表更有效地实现了这个目标。
If you logically want to create a collection where you can easily look up a customer by their ID, I would use some form of
IDictionary<int, Customer>
. That expresses what you're trying to achieve.Now you could use a list to do the same thing, and as leppie says for small datasets it will be about as fast or even faster - but for small datasets it'll be very fast anyway, so why do you care? I think it's more important to tell the reader of your code what you're trying to do with the collection - and a dictionary achieves that aim far more effectively than a list, IMO.
根据 MSDN 根据键从字典中获取项目“接近O(1) 操作。”另一方面,在列表上执行 Where循环遍历元素以查找匹配项。所以一般来说字典肯定会更快。
如果您想加快 Linq 操作速度,可以使用 索引 LINQ,它允许在集合上放置索引。
According to MSDN getting an item from a dictionary based on key "approaches an O(1) operation." On the other hand executing Where on a list loops through the elements to find matches. So generally dictionary will be definitely faster.
If you want to speed up Linq operations you can use Indexed LINQ which allows to put indexes on your collections.
对于小于 20 个项目的列表,字典/哈希表的开销将导致它比列表慢。
For lists smaller than 20 items, the overhead of a
Dictionary/Hashtable
will causes it to be slower than a list.LINQ 并不神奇。它仍然需要遍历列表来查找所需的元素。字典仍然会更快(对于合适大小的集合,正如 leppie 指出的那样)
LINQ isn't magic. It will still have to iterate through a list to find the element you want. Dictionary will still be faster (for collections of suitable size, as leppie points out)
LINQ 在此类操作中通常会较慢。但是,在足够小的集合上(例如您的示例),由于开销的差异,它可能会更快。然而,在足够小的集合上(例如您的示例),任一解决方案之间的差异都非常小,以至于字典查找或Where() 读取更自然的问题并不那么重要。
LINQ will generally be slower in this sort of operation. However, on a small enough set (such as your example), it'll likely be faster, due to the differences in overhead. However again, on a small enough set (such as your example) the difference between either solution is going to be so small that it won't matter as much as the question of whether dictionary look up or Where() reads more naturally.
您也许可以使用 SortedList 并对此集合执行二分搜索(考虑到它在第一次比较后消除了一半的集合)。
You could perhaps use a SortedList and perform binary search (considering it eliminates half the collection after the first comparison) on this collection.