Reputation: 39671
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
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
Reputation: 104065
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
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
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