C#列表<双>大小VS双[]尺寸 [英] C# List<double> size vs double[] size

查看:145
本文介绍了C#列表<双>大小VS双[]尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只是测试<一href="http://www.microsoft.com/downloads/details.aspx?FamilyId=A362781C-3870-43BE-8926-862B40AA0CD0&displaylang=en"相对=nofollow> CLR探查器从微软的,而我做了创建的列表1,000,000双打中有一个小程序。我检查了堆,和原来的名单,LT;>尺寸约为124KB(我不记得确切,但它周围的)。这确实震撼了我的世界,怎么会被它是否有百万双打是124KB?总之,在那之后我决定检查双[百万]。让我吃惊(这不是真的,因为这是我所期待的与List&LT;> = P),数组的大小为7.6MB。巨大的差异!

So I just was testing the CLR Profiler from microsoft, and I did a little program that created a List with 1,000,000 doubles in it. I checked the heap, and turns out the List<> size was around 124KB (I don't remember exactly, but it was around that). This really rocked my world, how could it be 124KB if it had 1 million doubles in it? Anyway, after that I decided to check a double[1000000]. And to my surprise (well not really since this is what I expected the with the List<> =P), the array size is 7.6MB. HUGE difference!!

来他们是如何不同?如何列表和LT;>管理的项目,它是如此(难以置信)内存高效?我的意思是,它不是像其他的7.5 MB是别的地方,因为应用程序的大小大约为3或4 KB更大的后,我创建了100万双。

How come they're different? How does the List<> manage its items that it's so (incredibly) memory efficient? I mean, it's not like the other 7.5 mb were somewhere else, because the size of the application was around 3 or 4 KB bigger after I created the 1 million doubles.

推荐答案

名单,其中,T&GT; 使用数组存储值/引用,所以我怀疑会有T&GT; 补充说除了那一点点开销名单,其中的大小有什么区别。

List<T> uses an array to store values/references, so I doubt there there will be any difference in size apart from what little overhead List<T> adds.

下面给出

var size = 1000000;
var numbers = new List<double>(size);
for (int i = 0; i < size; i++) {
   numbers.Add(0d);
}

堆看起来像这样的相关对象

the heap looks like this for the relevant object

0:000> !dumpheap -type Generic.List  
 Address       MT     Size
01eb29a4 662ed948       24     
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
662ed948        1           24 System.Collections.Generic.List`1[[System.Double,  mscorlib]]
Total 1 objects

0:000> !objsize 01eb29a4    <=== Get the size of List<Double>
sizeof(01eb29a4) =      8000036 (    0x7a1224) bytes     (System.Collections.Generic.List`1[[System.Double, mscorlib]])

0:000> !do 01eb29a4 
Name: System.Collections.Generic.List`1[[System.Double, mscorlib]]
MethodTable: 662ed948
EEClass: 65ad84f8
Size: 24(0x18) bytes
 (C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
65cd1d28  40009d8        4      System.Double[]  0 instance 02eb3250 _items    <=== The array holding the data
65ccaaf0  40009d9        c         System.Int32  1 instance  1000000 _size
65ccaaf0  40009da       10         System.Int32  1 instance  1000000 _version
65cc84c0  40009db        8        System.Object  0 instance 00000000 _syncRoot
65cd1d28  40009dc        0      System.Double[]  0   shared   static _emptyArray
    >> Domain:Value dynamic statics NYI
 00505438:NotInit  <<

0:000> !objsize 02eb3250 <=== Get the size of the array holding the data
sizeof(02eb3250) =      8000012 (    0x7a120c) bytes (System.Double[])

因此​​,名单,其中,双&GT; 为8000036字节,基础数组是8000012字节。这正好与通常的12个字节的开销为引用类型(阵列)和100万次8个字节的双打。最重要的是名单,其中,T&GT; 增加了开销为上述领域的另外24个字节

So the List<double> is 8,000,036 bytes, and the underlying array is 8,000,012 bytes. This fits well with the usual 12 bytes overhead for a reference type (Array) and 1,000,000 times 8 bytes for the doubles. On top of that List<T> adds another 24 bytes of overhead for the fields shown above.

结论:我没有看到任何证据表明名单,其中,双&GT; 将占用比的空间更小双[] 为元素的数相同。

Conclusion: I don't see any evidence that List<double> will take up less space than double[] for the same number of elements.

这篇关于C#列表&LT;双&GT;大小VS双[]尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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