willc2
willc2

Reputation: 39671

How to access system time finer than 1 second on the iPhone

The system time function time(0) gives me a resolution of 1 second, right?

Is there a finer-grained function?

I'm using it to determine the time interval between two events.

A line of code would help me greatly. It makes it easier to have something concrete to hang the concept on when I look in the official documentation.

Upvotes: 9

Views: 7024

Answers (4)

Kornel
Kornel

Reputation: 100120

NSDate has timeIntervalSinceDate: method, which returns double ("sub-millisecond precision over a range of 10,000 years" Apple says).

NSDate *start = [NSDate date];
…
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];

Upvotes: 4

zoul
zoul

Reputation: 104065

See CFAbsoluteTimeGetCurrent:

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
// do something you want to measure
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"operation took %2.5f seconds", end-start);

Should you find CFAbsouteTime too verbose, you can simply use double instead.

Upvotes: 17

melfar
melfar

Reputation: 360

self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval
                       target:self selector:@selector(drawView) userInfo:nil
                       repeats:YES];

That's a snippet from the OpenGL app template. If you're looking for a high resolution timer, it's probably what you need.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 754050

Did you look for gettimeofday()? That's the main POSIX function for sub-second resolution timing analogous to time().

See Native App Development for the iPhone for an illustration of its use.

Upvotes: 1

Related Questions