2

I try to draw rain and snow as particle system using Core Graphics. In simulator rendering proceeded fine but when I run my app on real device rendering is slow down.

So, advise me please approaches to increase particle system drawing performance on iPhone.

May be I should use OpenGL for this or CoreAnimation?

genpfault
  • 51,148
  • 11
  • 85
  • 139
e40pud
  • 336
  • 2
  • 18
  • I think you need to identify _where_ the slow-down is occurring. If it's in drawing code, then perhaps switching technologies would get a speed-up. You may be able to improve your algorithms without swapping APIs. Load up Shark and Instruments, and find out where your time is spent. –  Mar 29 '10 at 13:57

3 Answers3

2

OpenGL would be the lowest level to drop to for the rendering, so should offer the best performance (if done right). CoreAnimation would be close enough if there are not too many particles (the exact figure depends on other factors, but upto about 50 should be ok). When you say you're using CoreGraphics at the moment do you mean you're redrawing based on a timer? If so then CoreAnimation will definitely help you out - as long as you can seperate out each particle into a view. You could still use CoreGraphics to render the individual particles.

Are you using a physics engine to calculate the positions?

philsquared
  • 22,403
  • 12
  • 69
  • 98
  • > When you say you're using CoreGraphics at the moment do you mean you're redrawing based on a timer? Yes. I use timer. > Are you using a physics engine to calculate the positions? A little bit. I use few formulas for trajectory calculation. – e40pud Mar 29 '10 at 14:20
  • Maximum particles count will be ranged from 100 to 200 (maybe 300) – e40pud Mar 29 '10 at 19:13
  • Ok - one view per particle probably won't scale to that level - but you could probably still group nearby particles into a fewer number of views. Beyond that I think OpenGL ES is going to be necessary - but be prepared for a bit of a learning curve if you've not done it before. – philsquared Mar 30 '10 at 07:42
  • Thank you! What do you think about third libraries (such as Cocos2D)? Is it good idea look at it? – e40pud Mar 30 '10 at 08:43
  • I've not used them myself but I've seen Cocos2D used by someone else and it looks pretty good. Unless you're invested in writing your own high quality physics engine I'd go with Cocos2D or similar. – philsquared Mar 30 '10 at 13:18
2

If you are simply drawing a view with Core Graphics, then redrawing it every frame to reflect the movement of the particles, you will see terrible performance (for more, see this answer). You will want to go to OpenGL ES or Core Animation for the particle system animation.

My recommendation would be to look at CAReplicatorLayer, a new Core Animation layer type added in iPhone OS 3.0 and Snow Leopard. I've seen some impressive particle systems created just using this one layer type, without using much code. See the ReplicatorDemo sample application (for the Mac, but the core concepts are the same), or Joe Ricioppo's "To 1e100f And Beyond with CAReplicatorLayer" article.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
1

It's hard to give advice with little to no information about your implementation. One thing that is a major bottleneck on the iPhone from my experience are memory allocations. So if you're allocating new objects for each particle spawned, that would be the first thing you might want to fix. (Allocate a pool of objects and reuse them.)

kloffy
  • 2,928
  • 2
  • 25
  • 34
  • I reuse all objects after their lifetime is ended. – e40pud Mar 29 '10 at 14:23
  • If you are sure that the drawing code is the root of your performance problems, switching to OpenGL makes sense, as Phil pointed out. I just wanted to note that there might be room for optimization without rewriting the entire drawing logic. Efficient rendering of a particle system using OpenGL isn't trivial... – kloffy Mar 29 '10 at 15:05