C#List .ConvertAll效率和开销 [英] C# List .ConvertAll Efficiency and overhead

查看:497
本文介绍了C#List .ConvertAll效率和开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解了List的.ConvertAll扩展名.今天,我在工作中的代码中使用了几次,以将一大堆对象转换为其他一些对象的列表.看起来真的很好.但是,我不确定与仅迭代列表和转换对象相比,此方法的效率或速度有多高. .ConvertAll是否使用任何特殊方法来加快转换过程,还是只是无需建立循环即可转换List的简便方法?

I recently learned about List's .ConvertAll extension. I used it a couple times in code today at work to convert a large list of my objects to a list of some other object. It seems to work really well. However I'm unsure how efficient or fast this is compared to just iterating the list and converting the object. Does .ConvertAll use anything special to speed up the conversion process or is it just a short hand way of converting Lists without having to set up a loop?

推荐答案

没有比直接找到源头更好的方法了,字面上:)

No better way to find out than to go directly to the source, literally :)

http://referencesource.microsoft.com/# mscorlib/system/collections/generic/list.cs#dbcc8a668882c0db

如您所见,没有特殊的魔法在发生.它只是遍历列表,并通过您指定的转换器函数创建一个新项目.

As you can see, there's no special magic going on. It just iterates over the list and creates a new item by the converter function that you specify.

说实话,我不知道这种方法. .NET进行这种投影的更惯用的方法是通过在IEnumerable<T>上使用Select扩展方法,如下所示:source.Select(input => new Something(input.Name)).这样做的好处是三方面的:

To be honest, I was not aware of this method. The more idiomatic .NET way to do this kind of projection is through the use of the Select extension method on IEnumerable<T> like so: source.Select(input => new Something(input.Name)). The advantage of this is threefold:

  • 正如我所说的,这更像是偶语,ConvertAll可能是C#3.0之前的版本的剩余时间.无论如何,这都不是一种非常神秘的方法,ConvertAll是一个非常清晰的描述,但坚持使用其他人所知道的Select还是更好.
  • 它在 all IEnumerable<T>上可用,而ConvertAll仅在List<T>的实例上可用.不管是数组,列表还是字典,Select都可以使用.
  • Select是懒惰的.除非您对其进行迭代,否则它不会做任何事情.这意味着它返回一个IEnumerable<TOutput>,然后您可以通过调用ToList()转换为列表,如果您实际上不需要一个列表,则可以不调用它.或者,如果您只想转换和检索一百万个项目列表中的前两个项目,则只需执行source.Select(input => new Something(input.Name)).Take(2).
  • It's more idomatic as I said, the ConvertAll is likely a remnant of the pre-C#3.0 days. It's not a very arcane method by any means and ConvertAll is a pretty clear description, but it might still be better to stick to what other people know, which is Select.
  • It's available on all IEnumerable<T>, while ConvertAll only works on instances of List<T>. It doesn't matter if it's an array, a list or a dictionary, Select works with all of them.
  • Select is lazy. It doesn't do anything until you iterate over it. This means that it returns an IEnumerable<TOutput> which you can then convert to a list by calling ToList() or not if you don't actually need a list. Or if you just want to convert and retrieve the first two items out of a list of a million items, you can simply do source.Select(input => new Something(input.Name)).Take(2).

但是,如果您的问题纯粹是关于将整个列表转换为另一个列表的性能,那么ConvertAll可能会更快一些,因为它的通用性要比Select后跟ToList的通用性低(它知道列表具有大小,并且可以直接从底层数组按索引访问元素.)

But if your question is purely about the performance of converting a whole list to another list, then ConvertAll is likely to be somewhat faster as it's less generic than a Select followed by a ToList (it knows that a list has a size and can directly access elements by index from the underlying array for instance).

这篇关于C#List .ConvertAll效率和开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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