mach_absolute_time 快速容易的方法来检测一段ios 代码执行的效率函数

Quick Performance Measurements


原文链接 :http://iosdeveloperzone.com/2011/05/03/quick-performance-measurements/

A quick and easy way to measure the performance of a piece of iOS code is to dig down below all the Cocoa Touch stuff and use the low-level mach_absolute_time. This call returns a tick count that can be converted into nanoseconds using information obtained from themach_timebase_info call. The following code sketches out the technique:

add 

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

{

    uint64_t begin = mach_absolute_time();

    [selfdoSomething];

    uint64_t end = mach_absolute_time();

    NSLog(@"Time taken to doSomething %g s",

          MachTimeToSecs(end - begin));

}

-(void)doSomething

{

    for (int i = 0; i <1000; i++) {

        NSLog(@"test");

    }

}


The timebase values on the simulator, at least on my MacBook Air, are 1/1, however on the iPad and iPhone 4 they are 125/3; so don’t be lazy and hard code them.

Never assume that because something runs quickly on the simulator it will also run quickly on a target device. On two jobs last year I encountered code that took 100x longer to run an iPad than in the simulator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值