iOS 使用Instruments工具Time Profiler
2019-01-14 本文已影响28人
小盟城主
在项目运行时有时候会出现卡顿感,例如tableView
滑动出现卡顿,这个时候就需要追踪卡顿的来源,这个时候就用到Instruments
中的Time Profiler
来进行性能优化。
-
先预编译项目(
command+i
),然后在弹出来的Instruments工具中选择Time Profiler
Instruments面板
-
点击开始运行项目,为了测试这个性能我故意在cell里面添加了阻塞性能代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"测试数据%ld",indexPath.row];
for (int i = 0; i < 1000; i++) {
[cell addSubview:[UILabel new]];
}
return cell;
}
-
然后在运行的项目中滑动
tableView
会发现出现严重的卡顿感
Time Profiler
-
会发现
CPU
此刻处于暴增状态,接近占用100%,所以这个时候就需要找出造成CPU
消耗这么大的问题所以,在下面的面板中选中Call Tree
中的Separate by Thread
和Hide System Libraries
Time Profiler配置
-
接下来在线程中找到问题代码,然后双击定位到代码的位置
找到问题代码

注意
如果time profiler
中看不到方法名只能看到十六进制地址的解决办法
1、设置profiler
为debug
模式
首先打开Edit Scheme

设置
profiler
为debug

2、设置Debug Information Format
中对应的Debug
下为DWARF with dSYM File
。和查看crash log
文件一样需要使用dSYM
文件来解析方法名称,没有这个的话只能显示十六进制的地址。
