使用 NSMutableDictionary 与 NSMutableArray 造成的性能损失>
我正在考虑使用 NSMutableDictionary 代替我当前的 NSMutableArray。 这主要是出于 KVC/KVO 的原因。 该集合将在我的绘图方法的内循环中经历严重的变化。 如果我继续进行此替换,性能是否会受到重大影响?
干杯, 道格
I am considering using an NSMutableDictionary in place of my current NSMutableArray. This is primarily for KVC/KVO reasons. The collection will undergo heavy mutation within the inner loop of my drawing method. Can I expect to incur a significant performance hit if I go ahead with this replacement?
Cheers,
Doug
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唯一确定的方法就是测量。 我们没有人对 NSMutableDictionary 和 NSMutableArray 的实现如何工作有足够的了解,所以询问没有什么意义。
当然,您可能会遇到一些命中,因为字典必须进行简单数组不会进行的额外哈希处理。 这是否“重要”很难说。
再次测量。
The only way to be sure is to measure. None of us have enough knowledge about how NSMutableDictionary's and NSMutableArray's implementations work, so there's little point asking.
Granted, you could probably expect some hit since the dictionary has to do additional hashing that a simple array would not. Whether or not that is "significant" is hard to say.
Again, measure.
正如他们所说,你需要测试这些东西。 但是......下面的简单测试对我有启发性,让我了解在小尺寸高分配率示例的情况下 NSMutableDictionary 和 NSMutableArray 集合类之间的相对速度差异。
运行以下程序的时间为:(打开垃圾收集)(在最近的四核机器上)
NSMutableDictionary 4.624478 秒
NSMutableArray 1.806365 秒
(我认为绝对时间并不是那么重要,它只是相对时间,对于这些小尺寸的类来说更重要。对于较大尺寸的类,当然集合类的性质将占主导地位,例如 NSMutableDictionary应该是 O(1) 来找到一个元素,等等......)
As they say you need to test these things. But... the following simple test was instructive for me to get an idea of the relative difference in speed between the NSMutableDictionary and NSMutableArray collection classes in the case of small size high allocation rate examples.
Running the following program the times were: (with garbage collection on) (on a recent quad core machine)
NSMutableDictionary 4.624478 seconds
NSMutableArray 1.806365 seconds
(The absolute times I don't think are really all that important, its just the relative times which are more important for these small size classes. For larger size classes of course the nature of the collection class will dominate, eg NSMutableDictionary should be O(1) to find an element, etc... )
当你说“主要是出于 KVC/KVO 原因”时,你能详细说明一下吗?
如果您因严重突变下过多的 KVO 触发而遇到性能问题,请考虑在完成后自行触发 KVO 通知:
When you say "primarily for KVC/KVO reasons", can you elaborate?
If you're seeing performance problems due to excessive KVO firing under heavy mutation, consider firing the KVO notifications yourself once you're done: