调用 ToList() 时对性能有影响吗? [英] Is there a performance impact when calling ToList()?

查看:35
本文介绍了调用 ToList() 时对性能有影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ToList() 时,是否需要考虑性能影响?

When using ToList(), is there a performance impact that needs to be considered?

我正在编写一个查询来从目录中检索文件,即查询:

I was writing a query to retrieve files from a directory, which is the query:

string[] imageArray = Directory.GetFiles(directory);

然而,由于我喜欢使用 List<> 而不是,我决定放入...

However, since I like to work with List<> instead, I decided to put in...

ListimageList = Directory.GetFiles(directory).ToList();

那么,在决定进行这样的转换时是否应该考虑某种性能影响 - 或者仅在处理大量文件时才考虑?这是一个可以忽略不计的转换吗?

So, is there some sort of performance impact that should be considered when deciding to do a conversion like this - or only to be considered when dealing with a large number of files? Is this a negligible conversion?

推荐答案

IEnumerable.ToList()

是的,IEnumerable.ToList() 确实会影响性能,这是一个 O(n) 操作,尽管它可能只需要在性能关键操作中注意.

IEnumerable.ToList()

Yes, IEnumerable<T>.ToList() does have a performance impact, it is an O(n) operation though it will likely only require attention in performance critical operations.

ToList() 操作将使用 List(IEnumerable collection) 构造函数.此构造函数必须制作数组的副本(更常见的是 IEnumerable),否则原始数组的未来修改将在源 T[] 上发生变化,这也不会'一般是不可取的.

The ToList() operation will use the List(IEnumerable<T> collection) constructor. This constructor must make a copy of the array (more generally IEnumerable<T>), otherwise future modifications of the original array will change on the source T[] also which wouldn't be desirable generally.

我想重申,这只会对庞大的列表产生影响,复制内存块是一种执行速度非常快的操作.

I would like to reiterate this will only make a difference with a huge list, copying chunks of memory is quite a fast operation to perform.

您会注意到在 LINQ 中有几种以 As 开头的方法(例如 AsEnumerable()) 和 To(例如 ToList()).以 To 开头的方法需要像上面那样的转换(即可能会影响性能),而以 As 开头的方法不需要,只需要一些转换或简单操作.

You'll notice in LINQ there are several methods that start with As (such as AsEnumerable()) and To (such as ToList()). The methods that start with To require a conversion like above (ie. may impact performance), and the methods that start with As do not and will just require some cast or simple operation.

这里有更多关于 List 如何工作的细节,如果你有兴趣 :)

Here is a little more detail on how List<T> works in case you're interested :)

A List 还使用了一种称为动态数组的构造,它需要按需调整大小,此调整大小事件将旧数组的内容复制到新数组.所以它开始时很小,如果需要,会增加大小.

A List<T> also uses a construct called a dynamic array which needs to be resized on demand, this resize event copies the contents of an old array to the new array. So it starts off small and increases in size if required.

这就是容量Count 属性在 List.Capacity 指的是后台数组的大小,CountList中的item个数,总是<=容量.因此,当一个项目被添加到列表中,增加它超过 Capacity 时,List 的大小加倍并复制数组.

This is the difference between the Capacity and Count attributes on List<T>. Capacity refers to the size of the array behind the scenes, Count is the number of items in the List<T> which is always <= Capacity. So when an item is added to the list, increasing it past Capacity, the size of the List<T> is doubled and the array is copied.

这篇关于调用 ToList() 时对性能有影响吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆