tags:

views:

424

answers:

3

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?

+1  A: 

1 in your case. Adding an additional timer doesn't make your application run faster.

You have to make your code more efficient or fire the timer less often.

I don't know about the overhead of a timer, but it could be considerable with a fire rate that high. Timers aren't made for this, I suggest a thread as an alternative.

Georg
+1  A: 

It sounds like the number of timers isn't a problem. The overhead of NSTimers is basically inconsequential compared to the loading you are doing in your "bunch of routines". Don't worry about it for now. Use as many timers as you need. After that open up shark/instruments and see which methods you are spending the most time on.

Kailoa Kadano
+2  A: 

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

Adding timers does not change the behaviour of your app. That said, in the same way that adding thready to an essentially single process machine 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 seperate thread and run your (same) timer code in that. This should give you more controll.

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

Roger Nolan