mach_absolute_time 快速容易的方法来检测一段ios 代码执行的效率函数
2014-02-10 12:07
489 查看
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 the
mach_timebase_infocall.
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.
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- dos 日期时间格式设置使用小结(Date和Time)
- Insert Date and Time into Access
- 解析mysql中UNIX_TIMESTAMP()函数与php中time()函数的区别
- 使用 MySQL Date/Time 类型
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 探讨Android与iOS,我们将何去何从?
- php中time()和mktime()方法的区别
- PHP中time(),date(),mktime()区别介绍
- IOS 身份证校验详细介绍及示例代码
- Python计时相关操作详解【time,datetime】
- iOS NSDate中关于夏令时的坑
- iOS内存错误EXC_BAD_ACCESS的解决方法
- 从 Auto Layout 的布局算法谈性能
- 浅谈 MVC、MVP 和 MVVM 架构模式
- 深入解析 ObjC 中方法的结构
- 你真的了解 load 方法么?