inside our app we have to sample something in a high resolution.
i have seen that the regular NSTimer in objective c , cant go under 50ms.
we need to sample in a rate of about 1ms at least .
this is so strange that there is no 1 way to sample in 1ms when the iphone processor is more that 1ghz !
i had 2 ideas: 1. do a while(1) that maybe will give me the clock, or, use accelerometer sample(is he that fast? ) , i am sure both are not ok .
any idea to this simple problem ?
It’s really not simple task to implement precise timer of that resolution. To get highly precise timer you probably need to implement your own run loop. If it requires only single timer with now dynamic timer insertion/deletion then it’s implementation rather simple.
As you said, run the while(…) loop and inside there you need to sleep for some time. This is the most tricky part, cause sometimes the previously actual elapsed time could be bigger/smaller then the actual one so you have to compensate it.
This is very simplified version of my own runloop implementation. The main idea here is that you have to keep track of the actual elapsed time and compensate any time overflows.
Here is the sample output for the 1ms time step:
To get even more precise results run the timer on a separate thread with high priority.