Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many NSTimers is too many?

Tags:

iphone

nstimer

In an iPhone OS device, how many simultaneously running NSTimers would be too many? I have a bunch of routines being called by a single timer firing 25 times a sec, and things are pretty choppy, slow, and heavy. I am thinking about adding one or more extra NSTimers to distribute the work load a bit but I am concerned about how many would be okay to use. This may be dependent somewhat on the firing rate of each individual timer I realise, so lets suppose a firing rate of 20~30 times per second. So, how many NSTimers is too many?

like image 511
RexOnRoids Avatar asked May 21 '09 05:05

RexOnRoids


1 Answers

Internally, NSTimers are all implemented on a single hardware timer. The OS keeps a central list of timers for all running apps and simply schedules a single hardware timer for the soonest event.

Adding timers does not change the behavior of your app. That said, in the same way that adding thread to an essentially single process app makes runtime management easier, adding timers can help you partition your problem.

Given the times you quote, I'm guessing that this timer is the frameupdate sync for a game or something similar. I would suggest a couple of things:

  1. Split your current drawing code into a separate thread and run your (same) timer code in that. This should give you more control.

  2. Profile your code to see how much processing is performed in each of those 1/25s slots - it may be (and probably is) that you simply need to optimize your code.

like image 198
Rog Avatar answered Nov 15 '22 08:11

Rog