陈斌彬的技术博客

Stay foolish,stay hungry

iOS 检测代码执行效率

#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));  

    }