开发者

What's making NSArray access so slow?

开发者 https://www.devze.com 2023-01-16 05:35 出处:网络
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 us

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 us开发者_开发问答ing 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消