iOS中精确时间的获取
下面是一段无法查证出处的英文和自己的翻译
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中精确时间的获取的更多相关文章
- 关于iOS中的时间
两类 绝对时间 [NSDate date].CFAbsoluteTimeGetCurrent(),或者gettimeofday(). 返回的是从某一个时刻开始,度过的秒数.会随着用户设置的系统时间更改 ...
- Java中系统时间的获取_currentTimeMillis()函数应用解读
快速解读 System.currentTimeMillis()+time*1000) 的含义 一.时间的单位转换 1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微 ...
- 苹果浏览器和ios中,时间字符串转换问题
背景:在开发PC端项目和小程序时,遇到过一个时间字符串转化问题,在苹果浏览器和ios微信客户端里,"2018-10-15 18:20" 以 字符"-"拼接的时间 ...
- iOS中的时间和日期
怎么说?时间和日期不是了不起的属性.了不起的功能,但是,我们决不能够因此就“冷落”它. 一:怎么“搞到货”--如何获取时间.日期 //-=-==当前时间------默认显示“0时区”时间 NSDate ...
- 【转】Java中本地时间的获取方法--不错
原文网址:http://highforest.blog.51cto.com/125539/842496/ 熟悉Oracle数据库的人,应该知道:select to_char(sysdate,'yyyy ...
- iOS中如何根据UIView获取所在的UIViewController
原理 Responder Chain 事件的响应者链 大概的传递规则就是从视图顶层的UIView向下到UIViewController再到RootViewController再到Window最后到Ap ...
- Android平台之不预览获取照相机预览数据帧及精确时间截
在android平台上要获取预览数据帧是一件极其容易的事儿,但要获取每帧数据对应的时间截并不那么容易,网络上关于这方面的资料也比较少.之所以要获取时间截,是因为某些情况下需要加入精确时间轴才能解决问题 ...
- iOS中的日期和时间
转载于http://www.jianshu.com/p/ee279c175cf8 一.时间和日期计算 我们在应用开发中,时常需要和时间打交道,比如获取当前时间,获取两个时间点相隔的时间等等,在iOS开 ...
- iOS中获取各种文件的目录路径的方法
我们的app在手机中存放的路径是:/var/mobile/Applications/4434-4453A-B453-4ADF535345ADAF344 后面的目录4434-4453A-B453-4AD ...
随机推荐
- Redis安装创建
安装 下载,解压和安装: $ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar ...
- jfinal配置rails的数据表
鉴于rails的部署太可怕,所以有了使用rails的建表工具和migration,用jfinal来开发的想法,在此贴一下需要注意的地方 maven配置 <dependency> <g ...
- Java I/O学习(附实例和详解)
原文地址:http://blog.csdn.net/u013142781/article/details/50814649 一.Java I/O类结构以及流的基本概念 在阅读Java I/O的实例之前 ...
- 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回
假设你数据库有个A表: ID NAME 1 aaa 2 bbb 3 ccc 4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...
- DISPOSE_ON_CLOSE 和 EXIT_ON_CLOSE 的区别
If you have several JFrames open and you close one that has EXIT_ON_CLOSE it will close all the JFra ...
- Apache HttpClient
HpptClient特性 1. 基于标准.纯净的java语言.实现了Http1.0和Http1.1 2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE ...
- IGS_学习笔记07_IREP通过页面测试客户化Web Service调用(案例)
20150819 Created By BaoXinjian
- 白书 4.1.2 模运算的世界 P291
1.逆元 这里有个注意事项要说,就是当要求 (a-b)%m 的时候要注意不能直接 (a%m-b%m)%m 原因是得出的值有可能是负数,所以 (a%m-b%m+m)%m 才是正确的. //x,y是引用 ...
- openmp并行计算
#include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...
- apache 做http代理
1.修改 http.conf 文件 ,增加 监听端口 Listen 开启需要的扩展 LoadModule proxy_module modules/mod_proxy.so LoadModule pr ...