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

业务性能监控, 在人工的在业务的开始和结束处打点上报,然后后台统计达到监控目的,

是性能优化里比较重要的一个维度。具体来说就是测试方法操作执行的时间损耗,可能是同步

也可能是异步的。测试的方法大概有如下五种:

第一种: 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();
// dosomething
CFTimeInterval 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/

 

posted @ 2016-05-08 13:31  skyming  阅读(2040)  评论(0编辑  收藏  举报