Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is NSTimer expensive?

Tags:

iphone

I am using about 20 UIImageViews of small airplane images (50x50 pixels) doing simple animation on the iPhone screen. The animation is done by shifting the UIImageView center property at timer interval.

[NSTimer scheduledTimerWithTimeInterval:0.01
                                 target:self
                               selector:@selector(timerFired:)
                               userInfo:nil
                                repeats:YES]

What is the best practice: create one NSTimer and loop the 20 UIImageViews to set the center property when timer fired? Or should I just create one NSTimer per UIImageView object? Is NSTimer resource expensive?

like image 830
Gaius Parx Avatar asked Apr 09 '09 18:04

Gaius Parx


2 Answers

I don't think it's that resource intensive, but common sense would seem to dictate that using 1 timer is probably better than 20.

Looks like your timer is set to fire 100 times per second, which seems a bit excessive. Do you animate each sprite at every timer firing? Might want to try firing 20-30 times per second instead (or maybe even less).

You might want to look into the built in view animation functions as well. Seems like they would probably work very well for what you are doing.

like image 172
Eric Petroelje Avatar answered Sep 24 '22 02:09

Eric Petroelje


you could try both and use the built in profiling tools that ship with Xcode to gauge resource usage.

like image 44
NoCarrier Avatar answered Sep 23 '22 02:09

NoCarrier