How do I count time in iOS?

Norton Commander picture Norton Commander · Sep 22, 2012 · Viewed 7.2k times · Source

I don't want to set up a timer that "fires" (and does something) after a certain amount of time has passed.

Therefore, I'm not interested in the NSTimer class and its methods.

All I'm interested in is counting the time that passes by while, for example, some while-loop is executing. I could use NSDate as follows I guess :

NSDate currentDate = [[NSDate alloc] init];

while(someConditionIsTrue)
{
    // executing..
}

timeElapsed = [self timeIntervalSinceDate:currentDate];

NSLog(@"time elapsed was: %i", timeElapsed);

However, if I understand correctly, timeIntervalSinceDate: can only count seconds.

Is there a way I can count the time that is passing by in milliseconds?

In other words, what is the smallest unit I can count passing time in and how ?

Answer

max_ picture max_ · Sep 22, 2012

Look at CFAbsoluteTimeGetCurrent()

CFAbsoluteTime before = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime after = CFAbsoluteTimeGetCurrent();