首先导入#import <mach/mach_time.h>
然后封装方法
-
(CGFloat)CodeRunTime:(void (^)())CodeBlock{
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);uint64_t startTime = mach_absolute_time();
//要执行的代码块
CodeBlock();
uint64_t endTime = mach_absolute_time();uint64_t less = endTime-startTime;
uint64_t nan = (double)less * (double)timebase.numer/(double)timebase.denom;
return (CGFloat)nan/NSEC_PER_SEC;
}