下面是一段无法查证出处的英文和自己的翻译

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:

精确衡量一段ios代码执行效率一个简易快速的方法就是深入挖掘cocoa touch层下面的层次——采用底层调用mach_absolute_time。mach_absolute_time会返回CPU的tick count计数器(私下认为这是cpu的时间),返回的时间可以被转换成毫微秒级。下面附上底层调用mach_absolute_time的实现示范。

add (附上)

#import <mach/mach_time.h>//导入要调用的底层框架

double MachTimeToSecs(uint64_t time) //cpu tickcount 转换成时间的函数

{

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();  //用mach_absolute_time()获得时间

//下面是要测试的ios代码,

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

NSLog(@"test");

}

uint64_t end = mach_absolute_time();   //用mach_absolute_time()获得时间

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

MachTimeToSecs(end - begin));   //end-begin然后转换,就得到精确的执行时间

}

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.

(此段英文主要讲不同的测试设备得到的结果不同,由于楼主不晓得1/1,125/3什么意思所以这段就不翻译了)

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.

永远不要假设代码在模拟器上的执行效率和在目标设备(比如iPhone 5)上的执行效率一样。我去年遇见过一段代码在iPad上比在模拟器上多跑了100X。

iOS中精确时间的获取的更多相关文章

  1. 关于iOS中的时间

    两类 绝对时间 [NSDate date].CFAbsoluteTimeGetCurrent(),或者gettimeofday(). 返回的是从某一个时刻开始,度过的秒数.会随着用户设置的系统时间更改 ...

  2. Java中系统时间的获取_currentTimeMillis()函数应用解读

    快速解读 System.currentTimeMillis()+time*1000) 的含义 一.时间的单位转换 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微 ...

  3. 苹果浏览器和ios中,时间字符串转换问题

    背景:在开发PC端项目和小程序时,遇到过一个时间字符串转化问题,在苹果浏览器和ios微信客户端里,"2018-10-15 18:20" 以 字符"-"拼接的时间 ...

  4. iOS中的时间和日期

    怎么说?时间和日期不是了不起的属性.了不起的功能,但是,我们决不能够因此就“冷落”它. 一:怎么“搞到货”--如何获取时间.日期 //-=-==当前时间------默认显示“0时区”时间 NSDate ...

  5. 【转】Java中本地时间的获取方法--不错

    原文网址:http://highforest.blog.51cto.com/125539/842496/ 熟悉Oracle数据库的人,应该知道:select to_char(sysdate,'yyyy ...

  6. iOS中如何根据UIView获取所在的UIViewController

    原理 Responder Chain 事件的响应者链 大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Ap ...

  7. Android平台之不预览获取照相机预览数据帧及精确时间截

    在android平台上要获取预览数据帧是一件极其容易的事儿,但要获取每帧数据对应的时间截并不那么容易,网络上关于这方面的资料也比较少.之所以要获取时间截,是因为某些情况下需要加入精确时间轴才能解决问题 ...

  8. iOS中的日期和时间

    转载于http://www.jianshu.com/p/ee279c175cf8 一.时间和日期计算 我们在应用开发中,时常需要和时间打交道,比如获取当前时间,获取两个时间点相隔的时间等等,在iOS开 ...

  9. iOS中获取各种文件的目录路径的方法

    我们的app在手机中存放的路径是:/var/mobile/Applications/4434-4453A-B453-4ADF535345ADAF344 后面的目录4434-4453A-B453-4AD ...

随机推荐

  1. 异步等待时,在异步前弹出窗口的TIMER,不会TICK

    窗体中用FORMS.TIMER,TICK不会走.改用TIMERS.TIMER 则正常.

  2. [tty与uart]1.Linux中tty框架与uart框架之间的调用关系剖析

    转自:http://developer.51cto.com/art/201209/357501_all.htm 目录 1.tty框架 2.uart框架 3.自底向上 4.自顶向下 5.关系图 在这期间 ...

  3. C#中abstract和virtual区别

    在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Virtual方法(虚方法) virtual ...

  4. 黄聪:get_posts 函数 | wordpress

    get_posts 函数,简单的来讲是 get_post 的复数新形势,但因为是文章多篇提取,所以使用方法上却略有不同,支持众多参数选择需要提取的文章,在 CMS 主题中经常被用到,当然如果你对 Wo ...

  5. 黄聪:WordPress图片插件:Auto Highslide修改版(转)

    一直以来很多人都很喜欢我博客使用的图片插件,因为我用的跟原版是有些不同的,效果比原版的要好,他有白色遮罩层,可以直观的知道上下翻图片和幻灯片放映模式.很多人使用原版之后发现我用的更加帅一些,于是很多人 ...

  6. iphone Dev 开发实例10:How To Add a Slide-out Sidebar Menu in Your Apps

    Creating the Xcode Project With a basic idea about what we’ll build, let’s move on. You can create t ...

  7. Maven使用--打包和运行

        将项目进行编译.测试后,下一个重要步骤就是打包.简单执行命令mvn clean package进行打包.Maven会在打包前执行编译.测试等操作.     在打包后,执行安装任务install ...

  8. Android: 触屏fling/scroll/drag的区别及其详细过程

    Google了一下,终于搞清了touch screen下的几种操作模式(对应的是事件). 对于一个view, 常用的操作有点击(click)和长按(long press)二种.实际上,这些操作类型是A ...

  9. ADF_Starting系列1_JDeveloper IDE开发环境简介

    2013-05-01 Created By BaoXinjian

  10. Form_通过Zoom客制化跳转页面功能(案例)

    2012-09-08 Created By BaoXinjian