将此 ArrayList 转换为通用列表是否有效?
我正在编写的代码从非托管代码接收一个 ArrayList,并且此 ArrayList 将始终包含一个或多个 Grid_Heading_Blk 类型的对象。我考虑过将此 ArrayList 更改为通用列表,但我不确定转换操作是否会如此昂贵,以至于抵消使用通用列表的好处。目前,我只是在将 ArrayList 传递给将使用它的类之后运行一个 foreach (Grid_Heading_Blk in myArrayList)
操作来处理 ArrayList 内容。
我应该将 ArrayList 转换为通用类型列表吗?如果是这样,最有效的方法是什么?
The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will always contain one or more objects of type Grid_Heading_Blk. I've considered changing this ArrayList to a generic List, but I'm unsure if the conversion operation will be so expensive as to nullify the benefits of working with the generic list. Currently, I'm just running a foreach (Grid_Heading_Blk in myArrayList)
operation to work with the ArrayList contents after passing the ArrayList to the class that will use it.
Should I convert the ArrayList to a generic typed list? And if so, what is the most efficient way of doing so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
下面介绍了一种从 ArrayList 创建通用列表的高性能方法。
通过调用接受 int 的构造函数,后备存储仅分配一次。
与往常一样,您应该使用您通常使用的任何工具来测量性能。
Here's a stab at a performant way to create a generic list from an ArrayList.
By calling the constructor that accepts an int, the backing storage is allocated only once.
As always, you should measure the performance using whatever tools you normally use.
我经常使用此清单来评估像您这样的问题:
List
比更能揭示意图>数组列表
。因此,即使不考虑效率,第 2 项也已经取得了巨大胜利。要将
ArrayList
转换为List<>
,您必须迭代>ArrayList
一次并转换每个元素。foreach
正在进行隐式转换,因此开销仅存在于额外的迭代中。迭代序列两次将性能从
O(n)
降低到O(2n)
,仍然是O(n)
(数量级,而不是值,这对性能来说很重要)。因此,您可以认为这种变化是良性的。但是,如果实际上您所做的所有都是运行
foreach
,那么您应该直接使用ArrayList
- 将其更改为List<> ;
不会给你带来更多的表达能力。I often use this checklist to evaluate questions like yours:
List<Grid_Heading_Blk>
is far more intention-revealing thanArrayList
. So, without even considering efficiency, there is already a big win for item 2.To convert an
ArrayList
to aList<>
, you have to iterate over theArrayList
once and cast each element. Theforeach
is doing an implicit cast, so the overhead is only in the extra iteration.Iterating a sequence twice takes the performance from
O(n)
toO(2n)
, which is stillO(n)
(magnitude, not value, is what matters for performance). Therefore, you can consider the change benign.However, if literally all you are doing is running the
foreach
, you should just useArrayList
directly - changing it toList<>
buys you no more expressive power.为什么需要转换
ArrayList
?老实说,您的 foreach 循环似乎可以解决问题。是的,正如凯文上面所说,您要支付的唯一惩罚是拆箱 - 但就目前情况而言,这是非常简单的代码,并且您可能没有足够的网格标题来支付真正的性能损失。但如果你必须转换它,我想说,与其编写自己的 for 循环来转换为 List 泛型类型,不如使用采用 IEnumerable 类型的构造函数(某种 ArrayList 应该已经实现了。)
Why do you need to convert the
ArrayList
at all? To be honest, your foreach loop seems like it would do the trick. Yes, as Kevin says above the only penalty you'd be paying is unboxing - but as it stands it is pretty simple code and you probably don't have enough grid headings to pay a real performance hit.But if you must convert it I would say, rather than writing your own for loop to convert to the List generic type, it might be better to use the constructor which takes
IEnumerable
type (somethingArrayList
should implement already.)使用 ArrayList 的最大惩罚是拳击。
使用泛型你会得到:
1. 编译时安全
2. 泛型扩展
3. 消除将列表中的所有内容转换为对象类型的限制。
这些是您使用它们的优势。它们是有优势的,但如果您必须从 ArrayList 重新填充泛型,则可能不值得这样做,特别是如果您只是循环访问列表来获取对象。
The biggest penalty you have using ArrayLists is boxing.
With generics you get:
1. compile time safety
2. generics extensions
3. remove this limitation of having everything in the list convert to type object.
Those are the advantages you get to using them. They're advantage, but if you have to re-populate the generic from the ArrayList, it may not be worth doing, especially if you are just looping through the list to get the objects.
“高效”不是一个非此即彼的属性。这是相对的,就像大老鼠可能不比小象大一样。
这取决于你还在做什么。
您的情况可能会有所不同,但根据我的经验,虽然
ArrayList
可能比List
“慢”,但我从来没有在任何其他方面做的事情如此之少。非常明显。也就是说,让编译器为我进行类型检查是件好事,而且不必强制转换也很好。
"Efficient" is not an either-or property. It is relative, just as a big mouse is probably not bigger than a small elephant.
It depends on what else you are doing.
Your mileage may vary, but in my experience, while
ArrayList
may be "slower" thanList<T>
, I have never been doing so little else that it was in any way noticeable.That said, it is nice to have the compiler doing type-checking for me, and it is nice not having to cast things.
如果对象来自非托管代码,并且您不需要添加或删除对象,则 Grid_Heading_Blk 数组可能比 List 更有效。如果您可以不使用数组,那么使用 for 循环可能会比 foreach 稍快一些。
If the objects are coming from unmanaged code, and you don't need to add or remove objects, then an array of Grid_Heading_Blk might be more efficient than a List. If you can get away with using an array, using a for loop might be slightly faster than foreach.
您可以使用 Enumerable.OfType 筛选可转换为同一类型的 arrayList 的所有元素(TResult 的)方法
(基于 http://www.codeproject.com/Tips/68291/Convert-ArrayList-to-a-Generic-List)
You can filter all elements of arrayList that can be cast to the same type using Enumerable.OfType(Of TResult) Method
(based on suggestion in http://www.codeproject.com/Tips/68291/Convert-ArrayList-to-a-Generic-List )