对于大 NSDictionary 来说,objectForKey 慢吗?
- 假设我们有很大的NSDictionary,当我们要调用objectForKey方法时,它会在核心中进行大量操作来获取值吗?还是直接指向内存中的值?
- 它在核心中是如何工作的?
- Assume we have very big NSDictionary, when we want to call the objectForKey method, will it make lots of operations in core to get value? Or will it point to value in the memory directly?
- How does it works in core?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
核心基础的集合编程主题<的 CFDictionary 部分/a> (如果你想了解更多,你应该查看)指出:
这是维基百科关于哈希表的说法:
因此,性能取决于哈希的质量。如果它很好,那么访问元素应该是 O(1) 操作(即不依赖于元素的数量)。
编辑:
事实上,在进一步阅读核心基础的集合编程主题之后,苹果给出了您问题的答案:
The CFDictionary section of the Collections Programming Topics for Core Foundation (which you should look into if you want to know more) states:
This is what wikipedia has to say about hash tables:
The performance therefore depends on the quality of the hash. If it is good then accessing elements should be an O(1) operation (i.e. not dependent on the number of elements).
EDIT:
In fact after reading further the Collections Programming Topics for Core Foundation, apple gives an answer to your question:
NSDictionary
本质上是一个哈希表结构,因此用于查找的 Big-O 是 O(1)。但是,为了避免重新分配(并实现 O(1))复杂性,您应该使用dictionaryWithCapacity:
创建一个新的字典,其大小适合数据集的大小。NSDictionary
is essentially an Hash Table structure, thus Big-O for lookup is O(1). However, to avoid reallocations (and to achieve the O(1)) complexity you should usedictionaryWithCapacity:
to create a new Dictionary with appropriate size with respect to the size of your dataset.