简体   繁体   English

使用字典比2D数组需要更多的内存吗?

[英]Does using dictionary require more memory than a 2D array?

My requirement is that I am making an application for WP7 so I have to take care of memory consumption by objects. 我的要求是我正在为WP7编写应用程序,因此我必须照顾对象的内存消耗。 I m having data of about 1000 records that to hard coded data. 我有大约1000条记录的数据,这些记录都是硬编码的数据。 So dictionary will be easily accessible than 2D array but is it consume more memory than 2D array. 因此,字典比2D数组更容易访问,但是它比2D数组消耗更多的内存。 Also which will be fast in doing search. 也可以快速进行搜索。 Ya I know search will be fast in dictionary where we can easily get the value for corresponding key by giving key, but is it internally do the linear search. 是的,我知道在字典中搜索会很快,在字典中我们可以通过给出键轻松获得相应键的值,但是它是在内部进行线性搜索。

Go with dictionaries. 和字典一起去。 1000 records isn't a lot. 1000条记录不是很多。 And the code for searches in dictionaries is really simple: 字典中的搜索代码非常简单:

var val = dict[key];

whereas the code for searches in 2D arrays, well. 而用于2D数组的搜索代码也可以。 Are you sorting your arrays? 您要对数组排序吗? That could make finding stuff faster. 这样可以更快地找到东西。 But also more complicated than doing a linear search. 而且比进行线性搜索还要复杂。 You'd then need to make sure the sorted property stays invariant. 然后,您需要确保sorted属性保持不变。

Oh, and then you'll have to maintain yet another lookup implementation. 哦,然后您必须维护另一个查询实现。

Don't go there! 不要去那里!

When you run out of memory, then maybe you can start thinking about optimizing the dictionary to some other structure. 当内存不足时,也许您可​​以开始考虑将字典优化为其他结构。 But you won't run out of memory. 但是您不会用完内存。 YAGNI. 亚尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM