??在項(xiàng)目運(yùn)行時(shí)有時(shí)候會(huì)出現(xiàn)卡頓感,例如tableView滑動(dòng)出現(xiàn)卡頓,這個(gè)時(shí)候就需要追蹤卡頓的來源,這個(gè)時(shí)候就用到Instruments中的Time Profiler來進(jìn)行性能優(yōu)化。
-
先預(yù)編譯項(xiàng)目(
command+i),然后在彈出來的Instruments工具中選擇Time Profiler
Instruments面板 點(diǎn)擊開始運(yùn)行項(xiàng)目,為了測(cè)試這個(gè)性能我故意在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:@"測(cè)試數(shù)據(jù)%ld",indexPath.row];
for (int i = 0; i < 1000; i++) {
[cell addSubview:[UILabel new]];
}
return cell;
}
-
然后在運(yùn)行的項(xiàng)目中滑動(dòng)
tableView會(huì)發(fā)現(xiàn)出現(xiàn)嚴(yán)重的卡頓感
Time Profiler -
會(huì)發(fā)現(xiàn)
CPU此刻處于暴增狀態(tài),接近占用100%,所以這個(gè)時(shí)候就需要找出造成CPU消耗這么大的問題所以,在下面的面板中選中Call Tree中的Separate by Thread和Hide System Libraries
Time Profiler配置 -
接下來在線程中找到問題代碼,然后雙擊定位到代碼的位置
找到問題代碼

注意
如果time profiler中看不到方法名只能看到十六進(jìn)制地址的解決辦法
1、設(shè)置profiler為debug模式
首先打開Edit Scheme

設(shè)置
profiler為debug
2、設(shè)置Debug Information Format中對(duì)應(yīng)的Debug下為DWARF with dSYM File。和查看crash log文件一樣需要使用dSYM文件來解析方法名稱,沒有這個(gè)的話只能顯示十六進(jìn)制的地址。




