它是更好地了ToList()或的ToArray()的LINQ查询调用? [英] Is it better to call ToList() or ToArray() in LINQ queries?

查看:316
本文介绍了它是更好地了ToList()或的ToArray()的LINQ查询调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常碰到,我想EVAL,我声明它的查询权的情况下。这通常是因为我需要遍历它多次的的是昂贵的计算。例如:

I often run into the case where I want to eval a query right where I declare it. This is usually because I need to iterate over it multiple times and it is expensive to compute. For example:

string raw = "...";
var lines = (from l in raw.Split('\n')
             let ll = l.Trim()
             where !string.IsNullOrEmpty(ll)
             select ll).ToList();

这工作得很好。 但是:如果我不打算修改的结果,那么我还不如叫的ToArray()而不是了ToList( )

This works fine. But if I am not going to modify the result, then I might as well call ToArray() instead of ToList().

我不知道但是是否的ToArray()是通过首先调用了ToList(实施),而不仅仅是调用了ToList因此较少的内存效率()。

I wonder however whether ToArray() is implemented by first calling ToList() and is therefore less memory efficient than just calling ToList().

我是疯了吗?我是否应该打电话的ToArray() - 安全,该内存不会被分配两次的知识获得

Am I crazy? Should I just call ToArray() - safe and secure in the knowledge that the memory won't be allocated twice?

推荐答案

除非你只需要一个阵列,以满足你应该使用其他约束了ToList 。在大多数场景的ToArray 将拨出超过了ToList 更多的内存。

Unless you simply need an array to meet other constraints you should use ToList. In the majority of scenarios ToArray will allocate more memory than ToList.

两者都使用阵列进行存储,但了ToList 有一个更灵活的约束。它需要在阵列为至少一样大集合中的元素的数量。如果阵列是较大的,这是没有问题的。然而的ToArray 需要数组被精确尺寸的元素数。

Both use arrays for storage, but ToList has a more flexible constraint. It needs the array to be at least as large as the number of elements in the collection. If the array is larger, that is not a problem. However ToArray needs the array to be sized exactly to the number of elements.

要实现这个功能的ToArray 经常做一件比了ToList 更多的分配。一旦它具有一个数组,它是足够大它分配一个数组,是完全正确的大小和复印元素放回该阵列。这样可避免这种情况的唯一时间是在成长的算法为阵正好与元素需要被存储(绝对少数)的数量一致。

To meet this constraint ToArray often does one more allocation than ToList. Once it has an array that is big enough it allocates an array which is exactly the correct size and copies the elements back into that array. The only time it can avoid this is when the grow algorithm for the array just happens to coincide with the number of elements needing to be stored (definitely in the minority).

修改

一,两个人都问我有额外的未使用的内存中的名单,其中的后果; T> 值。

A couple of people have asked me about the consequence of having the extra unused memory in the List<T> value.

这是一个有效的关注。如果创建集合长期生活,是永远不会被创建后修改并有登陆的第二代堆的机会很高,那么你可能会更好走的额外拨款的ToArray 锋线。

This is a valid concern. If the created collection is long lived, is never modified after being created and has a high chance of landing in the Gen2 heap then you may be better off taking the extra allocation of ToArray up front.

在一般的,虽然我觉得这是较少见的情况。它更经常可以看到大量的的ToArray 要求其立即传送到内存的其他短暂用途在这种情况下了ToList 可以证明是更好的。

In general though I find this to be the rarer case. It's much more common to see a lot of ToArray calls which are immediately passed to other short lived uses of memory in which case ToList is demonstrably better.

这里的关键是要配置文件,配置文件,然​​后简要介绍一下多。

The key here is to profile, profile and then profile some more.

这篇关于它是更好地了ToList()或的ToArray()的LINQ查询调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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