为什么 NSFastEnumeration 很快?

发布于 2024-12-20 09:01:53 字数 547 浏览 1 评论 0原文

有谁知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-12-27 09:01:53

根据容器的不同,NSFastEnumeration 至少与 NSEnumerator 一样快,但通常要快得多,因为它涉及的方法调用较少。

不必为数组中的每个项目调用 nextObject,使用 NSFastEnumerator 数组可以立即返回 C 数组中的对象块。迭代 C 数组比执行大量方法调用要快得多,每次方法调用都查找并返回一个对象。

Depending on the container, NSFastEnumeration is at least as fast as NSEnumerator but usually much faster since it involves fewer method calls.

Instead of having to call nextObject for every item in the array, with NSFastEnumerator 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.

与之呼应 2024-12-27 09:01:53

快速枚举并不比任何“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文