为什么 NSFastEnumeration 很快?
有谁知道 NSFastEnumeration
确实比使用
NSEnumerator
或(对于数组)使用整数计数器并循环遍历元素?
如果确实更快,那么它是如何达到这个速度的呢?
或者“快”实际上是指编写迭代代码的速度更快?
提前致谢。
Does anybody know whether NSFastEnumeration
is really faster (as in run-time performance) than using NSEnumerator
or (for arrays) using an integer counter and loop through the elements?
If it is indeed faster, how does it achieve that speed?
Or perhaps the "fast" actually refers to faster in writing the iteration code?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据容器的不同,
NSFastEnumeration
至少与NSEnumerator
一样快,但通常要快得多,因为它涉及的方法调用较少。不必为数组中的每个项目调用
nextObject
,使用NSFastEnumerator
数组可以立即返回 C 数组中的对象块。迭代 C 数组比执行大量方法调用要快得多,每次方法调用都查找并返回一个对象。Depending on the container,
NSFastEnumeration
is at least as fast asNSEnumerator
but usually much faster since it involves fewer method calls.Instead of having to call
nextObject
for every item in the array, withNSFastEnumerator
the array can give back a block of objects in a C array at once. Iterating that C array is way faster than having to do lots of method calls, which each look up and return just one object.快速枚举
并不比任何“for”循环更快。从一般意义上来说,这是不可能的,因为基本循环已经尽可能优化了。相反,
快速枚举
比“for”或“while”循环更快,后者使用 Objective-C 方法调用从 Objective-C 对象的每次迭代中获取数据。它通过批量获取数据来实现这一点,从而减少每次迭代调用方法的开销。从循环内部删除方法调用也有编译器优化的好处。
Fast enumeration
is not faster than any "for" loop. In a general sense, that is impossible because basic loops are already as optimized as they can be.Instead,
fast enumeration
is faster than a "for" or "while" loop that fetches data on every iteration from an Objective-C object using an Objective-C method call.It does this by fetching the data in batches, reducing the overhead of calling a method on each iteration. Removing the method call from the inner part of the loop also has compiler optimisation benefits.