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

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. 【NCDC数据】获取 hadoop权威指南3中的NCDC数据

    vi getNcdcBigData.sh 内容如下: #!/bin/bash for i in {1901..2014} do cd /home/xxxx/hapood/ncdc wget --exe ...

  2. nginx的https配置

    测试自签名的ssl证书 首先执行如下命令生成一个key openssl genrsa -des3 - 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次reload ...

  3. 【linux】之安装mysql常用配置

    下载mysql地址 http://dev.mysql.com/downloads/mysql/ 选择下面这个 查看是否存在mysql安装包 rpm -qa|grep -i mysql 删除mysql安 ...

  4. 文件压缩与挤压ZIP

    /// <summary> /// Zip压缩与解压缩 /// </summary> public class ZipHelper { /// <summary> ...

  5. 免安装Oracle客户端使用PLSQL Developer 7/8 连接Oracle10/11g

    众所周知,Oralce的客户端几百兆太大,网上也有许多DIR的处理.这里的处理使用官方提供ORALCE工具包Instant Client Package! 下载地址:http://www.oracle ...

  6. [Freescale]E9学习笔记-LTIB安装配置

    转自:http://blog.csdn.net/girlkoo/article/details/44535979 LTIB: Linux Target Image Builder Freescale提 ...

  7. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

  8. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...

  9. JadClipse eclipse反编译插件

    A.下载JadClipse,http://jadclipse.sourceforge.net/wiki/index.php/Main_Page#Download,注意选择与eclipse版本一致的版本 ...

  10. Cassandra安装及其简单试用

    官方主页:http://cassandra.apache.org/ 简介: The Apache Cassandra Project develops a highly scalable second ...