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

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. 获取网页URL地址及参数等的两种方法(js和C#)

    转:获取网页URL地址及参数等的两种方法(js和C#) 一 js 先看一个示例 用javascript获取url网址信息 <script type="text/javascript&q ...

  2. 折腾Ipython

    1. 用easy_install安装吧 [root@host python]# easy_install IPython Searching for IPython Reading https://p ...

  3. Tomcat服务器搭建

    一.JDK环境搭建 二.tomcat下载安装 三.tomcat服务启动 cmd> net start  tomcat8 四.查看tomcat服务器启动情况: http://localhost:8 ...

  4. .NET简谈构件系统开发模式

    转自[王清培] http://www.cnblogs.com/wangiqngpei557/archive/2011/06/14/2080416.html 在本人的“.NET简谈插件系统开发模式”一文 ...

  5. 黄聪:如何阻止iframe里引用的网页自动跳转

    今天做了个网页,要在网页里设置一个iframe,然后套用其他的网站.使用http://luanqi-cat.blogbus.com 这个网址的时候,出现了莫名其妙的问题,我的网页居然会强制自动跳转到这 ...

  6. 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)

    C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...

  7. Java面试必备知识

    JAVA面试必备知识 第一,谈谈final, finally, finalize的区别. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可 ...

  8. mac下反编译android apk

    所需要的工具 http://pan.baidu.com/disk/home#path=%252Fandroid%252Fdecompile%252Fapktool-all apktool用于将资源文件 ...

  9. 1小时vpn coding让开发更简单 或https://www.imfreevpn.org/

  10. Python进阶06 循环对象

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 这一讲的主要目的是为了大家在读Python程序的时候对循环对象有一个基本概念. 循 ...