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

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

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

也多是异步的。测试的方法大概有以下五种:github

第一种: NSDate 精确度多是微秒(μs)性能优化

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

第二种:clock_t 精确度多是微秒(μsapp

 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/

相关文章
相关标签/搜索