在iPad或手机上测量代码内的EXACT执行时间的代码?

是使用mach_absolute_time和下面的金鹰解释简单的NSDate方法之间的任何区别?

下面是使用mach方法的一个很好的解释

我如何精确计算在iPhone上调用函数需要多长时间?

测量库呼叫和回叫之间的时间

loop { NSDate *start = [NSDate date]; // a considerable amount of difficult processing here // a considerable amount of difficult processing here // a considerable amount of difficult processing here NSDate *methodFinish = [NSDate date]; NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:start]; NSLog(@"Execution Time: %f", executionTime); } 

应该pipe用。

在之前的回答中,我已经实现了一个简单的课程来衡量时间

怎么运行的:

 ABTimeCounter *timer = [ABTimeCounter new]; [timer restart]; //do some calculations [timer pause]; //do some other staff [timer resume]; //other code //You can measure current time immediately NSLog(@"Time left from starting calculations: %f seconds",[timer measuredTime]); [timer pause]; 

你的.h文件应该是这样的:

 @interface ABTimeCounter : NSObject @property (nonatomic, readonly) NSTimeInterval measuredTime; - (void)restart; - (void)pause; - (void)resume; @end 

.m文件:

 @interface ABTimeCounter () @property (nonatomic, strong) NSDate *lastStartDate; @property (nonatomic) BOOL isCounting; @property (nonatomic, readwrite) NSTimeInterval accumulatedTime; @end @implementation ABTimeMeasure #pragma mark properties overload - (NSTimeInterval) measuredTime { return self.accumulatedTime + [self p_timeSinceLastStart]; } #pragma mark - public - - (void) restart { self.accumulatedTime = 0; self.lastStartDate = [NSDate date]; self.isCounting = YES; } - (void) pause { if (self.isCounting){ self.accumulatedTime += [self p_timeSinceLastStart]; self.lastStartDate = nil; self.isCounting = NO; } } - (void) resume { if (!self.isCounting){ self.lastStartDate = [NSDate date]; self.isCounting = YES; } } #pragma mark - private - - (NSTimeInterval) p_timeSinceLastStart { if (self.isCounting){ return [[NSDate date] timeIntervalSinceDate:self.lastStartDate]; } else return 0; } @end