English 中文(简体)
搜索词典的替代词典
原标题:Alternative to Dictionary for lookup

I have requirement to build lookup table. I use Dictionary It s contain 45M long and 45M int . long as key and int as value . the size of collection is (45M*12) where long is 8 byte and int is 4 byte The size about 515 Mbyte . But in fact the size of process is 1.3 Gbyte . The process contains only this lookup table. Mat be, is there alternative to Dictionary ??

Thx 十进

问题回答

How much effort are you willing to spend?
You could use a

KeyValuePair<long,int>[] table = new KeyValuePair<long,int> [45 M];

然后在第一列( loong keey ) 上对此项进行排序,并使用二进制搜索查找您的值。

您可以使用一个分类列表, 而不是一个词典, 该词典将提高内存效率, 但可能略微降低 CPU 效率, 忽略了测量内存的问题, 以及为什么您需要在 1 中装入如此多的数据。 )

字典中有一个隐藏在数据上的基本数组, 但数组的大小 < em> must 大于您拥有的项目数量, 这是字典的查找速度的来源。 事实上, 底部数组的大小应该比项目数量( 25\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

这个字典消耗的内存比你能允许的要多,还是你只是好奇为什么它比你想的要多?你还有其他选择(其他答案和评论已经列出一些),这些选择会减少内存,但也会更慢。你没有记忆问题了吗?

if your range is limited to max long values of 10^12, then a problem in regards to space is that you must use longs because you only need a few bits more than an int can hold. If that s the case you could do something like this: Store your data in an array of 512 Dictionary

 var myData = new Dictionary<int,int>[512];

引用与一个长值相关的内注( 此处我称之为“ 密钥 ” ), 您会做以下操作 :

myData[key & 511].Add((int) (key >> 9), intValue);
int result = myData[(int) (key & 511)][(int) (key >> 9)];

您创建了多少词典, 以及位盘中所使用的比特折叠中的位数, 可能需要调整以适应您数据的真实天体 。 使用这个方法会将您的内存用量减少大约三分之一

另一种方法,假设数据是静态的:使用两个分类阵列- 一个长阵列和一个内插数组。 请确定在索引N中的条目是索引N中的键值。 使用 < a href="http://msdn. microsoft. com/ en- us/library/ system. array. binarysearch. aspx" rel = “ nofollow” > Array. BinarySearch 查找您正在寻找的关键值 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签