views:

105

answers:

2

I'm looking for high-resolution timing code for iPhone, in order to do some performance timings. I'd like to write code like this:

HighResolutionTimer* myTimer = [[HighResolutionTimer alloc]init];
[myTimer start];
[self doSomeLengthyOperation];
NSLog( @"doSomeLengthyOperation took %f seconds", [myTimer elapsedTime] );
+3  A: 

Use NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate] to get a start time, and then NSLog (@"Operation took %f seconds.", [NSDate timeIntervalSinceReferenceDate] - startTime); at the end.

lucius
That works perfectly, thanks! I didn't realize NSDate had this level of precision.
zpasternack
+2  A: 

Look into mach_absolute_time() in the mach/mach_time.h header.

Don't use NSDate. NSDate isn't even guaranteed to not go backwards occasionally, when ntp does its thing.

hotpaw2