iOS 计算代码块执行时间

#import <mach/mach_time.h> // for mach_absolute_time

double MachTimeToSecs(uint64_t time)
{
    mach_timebase_info_data_t timebase;
    mach_timebase_info(&timebase);
    return (double)time * (double)timebase.numer /  (double)timebase.denom / 1e9;
}

- (void)profileDoSomething
{  
    uint64_t begin = mach_absolute_time();
    [self doSomething];
    uint64_t end = mach_absolute_time();
    NSLog(@"Time taken to doSomething %g s", MachTimeToSecs(end - begin));
}

方法二:

方法一:使用系统时间
NSDate* tmpStartData = [[NSDate date] retain];
//You code here...
double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];
NSLog(@">>>>>>>>>>cost time = %f ms", deltaTime*1000);


第二种方法同样精确!!!!!!!

展开阅读全文

没有更多推荐了,返回首页