gpt4 book ai didi

objective-c - 为什么不使用优化 (-O0) 的 NSArray 访问速度比使用 (-O1, -O2, -O3, -Os, -Ofast) 更快?

转载 作者:行者123 更新时间:2023-12-03 16:36:24 27 4
gpt4 key购买 nike

摘要

根据我的测试,使用 -O0 时 NSArray 的平均访问时间最快。我的测试代码访问长度为 0 到 10000 的所有数组的每个元素。

循环每个数组:

start = mach_absolute_time();
for (string in array){}
arrayDur = mach_absolute_time() - start;

每个元素的访问时间:

  • -O0 ~7ns
  • -O1 ~12ns
  • -O2 ~12ns
  • -O3 ~12ns
  • -Os ~12ns
  • -Ofast ~12ns

图表:

不同长度数组的平均访问时间

Blue = -O0, Red = -Ofast

其中蓝色为 -O0,红色为 -Ofast,x 轴是数组中的元素,y 轴是访问元素的平均时间(以 ns 为单位)。

问题:

积极的优化(-Ofast)应该更快或与没有优化(-O0)一样快,但这表明,对于 NSArray 来说,优化实际上会减慢访问时间。为什么会这样?这怎么能被认为是一种优化?

最佳答案

将 manecosta 的评论转化为答案:

尽管您的帖子非常具体,但我认为您会发现 NSArray 根据其包含的元素数量等进行内部优化。

查看 ridiculous fish.com blog 上的这篇精彩帖子,观察 NSArray 的一些奇怪之处

这篇博文的要点之一是:

So it sure looks like CFArray is switching data structure implementations around 30,000 elements. And I believe there's lots more implementations that I haven't discovered, for smaller arrays and for immutable arrays.

关于objective-c - 为什么不使用优化 (-O0) 的 NSArray 访问速度比使用 (-O1, -O2, -O3, -Os, -Ofast) 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com