iOS检验代码执行效率的方法

2018-12-24  本文已影响11人  fortitude1990
#import <mach/mach_time.h>
 
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)doSomething
 
{
 
    uint64_t beginTime = mach_absolute_time();
 
    // 在这里放置你要检测效率的代码
 
    uint64_t endTime = mach_absolute_time();
 
    NSLog(@"Time taken to doSomething %g s",MachTimeToSecs(endTime - beginTime));
 
}
上一篇下一篇

猜你喜欢

热点阅读