iOS 性能优化之业务性能监控

来源:互联网 发布:西语助手mac版 编辑:程序博客网 时间:2024/06/01 08:23

 

 

第一种: NSDate 精确度可能是微秒(μs)

NSDate* tmpStartData = [NSDate date];//You code here...double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData];NSLog(@"cost time = %f s", deltaTime);

第二种:clock_t 精确度可能是微秒(μs

 clock_t start = clock();// dosomething clock_t end = clock(); NSLog(@"时间损耗 %f s", (double)(end - start)/CLOCKS_PER_SEC); 

第三种:CFAbsoluteTime 精确度可能是微秒(μs)

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();//You code here...CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();NSLog(@"cost time = %f s", end - start); //s 

第四种:CFTimeInterval 精确度纳秒(ns)

CFTimeInterval start = CACurrentMediaTime();// dosomethingCFTimeInterval end = CACurrentMediaTime();NSLog(@"时间损耗 = %f s", end - start); 

第五种:mach_absolute_time 精确度纳秒(ns) 

  uint64_t start = mach_absolute_time ();  // operation();  uint64_t end = mach_absolute_time ();  uint64_t elapsed = 1e-9 *(end - start); 

 

以上五种方法,实际可用的是最后两种,这五种都有什么关系呢?

NSDate -> gettimeofday  -> mach_absolute_time

也就是说最终的来源还是 mach_absolute_time, gettimeofday 加入的时间同步机制。

CSDN:http://blog.csdn.net/skymingst/article/details/41892445

 

mach_absolute_time 详解

http://southpeak.github.io/blog/2014/09/23/xing-neng-yu-shi-jian/

clock_t 是不可靠的

http://www.cnblogs.com/chenyadong/archive/2011/12/03/2274783.html

 

附录:

mach_absolute_time 比较严格些的时间检测方法见示例代码,中间参考了官方QA样例

特点:

1. 增加了Block形式支持 --不推荐使用 T_T

2. 支持单元测试标题输出

3. 支持同步、异步测试

4. 纳秒级精确度,默认是毫秒输出,精确度微秒

https://github.com/skyming/iOS-Performance-Optimization

 

首发:

http://skyming.me/2016/05/08/iOS-Performance-Optimization-Time-md/


0 0
原创粉丝点击