是什么让 NSArray 访问如此缓慢?

发布于 2024-09-24 02:10:24 字数 765 浏览 1 评论 0原文

我正在为 iPhone 应用程序制作图形效果,模拟旧演示场景中的低分辨率等离子效果。我的屏幕上有 600 个方块,它们正在尽可能快地更新。由于我(目前)使用的是 CoreGraphics,因此我可以获得非常稳定的 11FPS,而不会冻结

——但是当我尝试添加一个简单的 NSArray 查找时,iPhone 以及模拟器每隔几秒就会冻结几秒钟。我在仪器中运行了这个,它说存在泄漏——泄漏与 NSAutoReleasePool 有关,但这并没有真正的帮助。 (我在每个更新帧的开始处创建一个 NSAutoReleasePool ,并在最后耗尽该池。)

通过排除过程,我已经能够将其范围缩小到导致速度减慢的一行。此行发生在每帧更新的 600 个块精灵中:

//SLOOWWWNESS /LEAKING NSAUTORELEASEPOOL IS here (freezing every second or so)
UIColor *color = [palette objectAtIndex:colorNum];

//BUT DOESNT HAPPEN HERE... (works at full speed)
UIColor *color = [UIColor colorWithRed:0.25f green:0.25f blue:colorNum/15.0 alpha:1.0f];

... 其中,调色板是精灵中的 NSArray 属性,并且包含我创建的 UIColor 对象的列表。这个 UIColor 对象数组仅在应用程序启动时创建一次,并且所有精灵都使用相同的数组。

有人有什么想法吗?

I'm working up a graphics effect for an iPhone app that simulates a low-res plasma effect from old demoscene days. I've got 600 squares on screen that are updating as fast as possible. Since I am using CoreGraphics (for now), I can get a very constant 11FPS with no freezing--

BUT when I try to add one simple NSArray lookup, the iPhone as well as the simulator freezes for several seconds every few seconds. I've run this in instruments and it says there is a leak -- the leak has to do with NSAutoReleasePool, but that doesn't really help. (I am creating an NSAutoReleasePool at the beginning of every update frame and draining the pool at the end.)

By process of elimination, I've been able to narrow it down to one line that's causing the slowdown. This line happens for each of the 600 block sprites each frame update:

//SLOOWWWNESS /LEAKING NSAUTORELEASEPOOL IS here (freezing every second or so)
UIColor *color = [palette objectAtIndex:colorNum];

//BUT DOESNT HAPPEN HERE... (works at full speed)
UIColor *color = [UIColor colorWithRed:0.25f green:0.25f blue:colorNum/15.0 alpha:1.0f];

... where palette is a NSArray property in the sprite, and contains a list of UIColor objects that I've created. This array of UIColor objects is created only once, when the app launches, and all sprites are using the same array.

Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

纵山崖 2024-10-01 02:10:24

什么是颜色编号?它可能是数组范围之外的一些疯狂的值吗?

调色板是“保留”属性吗...调色板对象确实已被保留,对吗?它是综合的,还是您实现了自己的 getPalette 方法?

What is colorNum? Could it ever be some crazy value outsides the array's bounds?

Is palette a 'retain' property... the palette object really has been retained, right? Is it synthesized, or have you implemented your own getPalette method?

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