是列表<>元素顺序位于堆之类的数组中? [英] Are List<> elements sequentially located in heap like array?

查看:76
本文介绍了是列表<>元素顺序位于堆之类的数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C#,并且基本上知道数组和 List s之间的区别,最后一个是通用的并且可以动态增长,但是我想知道:

I'm learning C# and basically know the difference between arrays and Lists that the last is a generic and can dynamically grow but I'm wondering:


  • List 元素顺序位于堆(如数组)中,或者每个元素都随机位于

  • ,如果是这样,是否会影响访问和访问速度?从内存中检索数据?

  • ,如果的确是这样,这是否会使数组比 List s快一点?

  • are List elements sequentially located in heap like array or is each element located "randomly" in a different locations?
  • and if that is true, does that affect the speed of access & data retrieval from memory?
  • and if that is true, is this what makes arrays a little faster than Lists?

推荐答案

让我们首先看看第二和第三个问题:

Let's see the second and the third questions first:


,如果是,那么会影响访问速度&从内存中检索数据?

and if that true does that affect the speed of access & data retrieval from memory ?

,如果是这样,这会使数组比列表快一点吗?

and if that true is this what makes array little faster than list ?

.NET中只有一种类型的本机集合(.NET是CLR,所以是运行时):数组(从技术上讲,如果您考虑使用 string 一种集合类型,然后有两种本机类型的集合:-))(技术上第2部分:并非所有您认为是数组的数组都是本机数组...仅一维的)基于0的数组是本机数组,不是 T [,] 类型的数组,而第一个元素的索引不为0的数组不是)。其他所有集合(除了 LinkedList<> 之外)都建立在其上。如果您查看带有 IlSpy List< T> ,您会发现它的底部有一个 T [] ,并为 Count 添加了 int T []。Length Capacity )。显然,数组要比 List< T> 快一点,因为要使用它,您的间接寻址要少一些(直接访问数组,而不是访问要访问的数组

There is only a single type of "native" collection in .NET (with .NET I mean the CLR, so the runtime): the array (technically, if you consider a string a type of collection, then there are two native types of collections :-) ) (technically part 2: not all the arrays you think that are arrays are "native" arrays... Only the monodimensional 0 based arrays are "native" arrays. Arrays of type T[,] aren't, and arrays where the first element doesn't have an index of 0 aren't) . Every other collection (other than the LinkedList<>) is built atop it. If you look at the List<T> with IlSpy you'll see that at the base of it there is a T[] with an added int for the Count (the T[].Length is the Capacity). Clearly an array is a little faster than a List<T> because to use it, you have one less indirection (you access the array directly, instead of accessing the array that accesses the list).

让我们看看第一个问题:

Let's see the first question:


是否列出了元素

does List elements sequentially located in heap like array or each element is located randomly in different locations?

内部基于数组,显然 List<> 像数组一样存储其元素,因此存储在连续的内存块中(但请注意,使用 List< SomeObject> ,其中 SomeObject 是引用类型,该列表是引用而不是对象的列表,因此引用被放在连续的内存块中(我们将忽略计算机的高级内存管理,连续的内存块一词并不准确),最好说连续的地址块))

Being based on an array internally, clearly the List<> memorizes its elements like an array, so in a contiguous block of memory (but be aware that with a List<SomeObject> where SomeObject is a reference type, the list is a list of references, not of objects, so the references are put in a contiguous block of memory (we will ignore that with the advanced memory management of computers, the word "contiguous block of memory" isn't exact", it would be better to say "a contiguous block of addresses") )

(是的,甚至是 Dictionary<> HashSet<> 建立在数组的顶部。相反,可以在不使用数组的情况下构建类似树的集合,因为它与 LinkedList

(yes, even Dictionary<> and HashSet<> are built atop arrays. Conversely a tree-like collection could be built without using an array, because it's more similar to a LinkedList)

其他一些细节: CIL 语言(已编译的.NET程序中使用的中间语言)有四组指令与本机数组一起使用:

Some additional details: there are four groups of instructions in the CIL language (the intermediate language used in compiled .NET programs) that are used with "native" arrays:


  • Newarr

Ldelem 和家庭 Ldelem _ *

Stelem 和家人 Stelem _ *

ReadOnly (不要问我,我不知道,文档也不清晰)

ReadOnly (don't ask me its use, I don't know, and the documentation isn't clear)

如果您查看 OpCodes.Newarr ,您会在XML文档中看到以下注释:

if you look at OpCodes.Newarr you'll see this comment in the XML documentation:

// Summary:
//     Pushes an object reference to a new zero-based, one-dimensional array whose
//     elements are of a specific type onto the evaluation stack.

这篇关于是列表&lt;&gt;元素顺序位于堆之类的数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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