通话是有一个性能的影响,当了ToList()? [英] Is there a performance impact when calling ToList()?

查看:175
本文介绍了通话是有一个性能的影响,当了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(目录);

然而,因为我喜欢与列表&LT配合工作;> 相反,我决定把中...

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

列表&LT;串GT;图像列表= Directory.GetFiles(目录).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?

推荐答案

是,<一个href=\"http://msdn.microsoft.com/en-us/library/bb342261.aspx\"><$c$c>IEnumerable<T>.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()操作将使用 列表(IEnumerable的&LT; T&GT;集合) 构造函数。此构造必须使数组的副本(更通常的IEnumerable&LT; T&GT; ),原数组的,否则日后修改将改变源 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发现有几种方法,以开始为(如的 AsEnumerable() )和(如<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/bb342261.aspx> 了ToList() )。与启动方法需要像上面转换(即可能会影响性能),并与开始为不和只是需要一些转换或操作简单方便。

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.

下面是一个更详细一点如何列表&LT; T&GT; 工程如果你有兴趣:)

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

A 列表&LT; T&GT; 还采用了一种叫做动态数组,需要根据需求进行调整结构,这个调整大小事件拷贝旧数组的新内容数组。因此,开始了小增加大小,如果需要

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.

这是 容量 <之间的差异/ A>和 计数 属性上 列表&LT; T&GT; 容量指的是幕后的数组的大小,计数是项目在<$ C $数C>列表&LT; T&GT; 始终是&LT; =容量。所以,当一个项目被添加到列表中,增加它过去容量列表&LT规模; T&GT; 是一倍,阵列复制。

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天全站免登陆