优化TableView索引排序的性能
来源:互联网 发布:域名备案转入阿里云 编辑:程序博客网 时间:2024/04/29 22:56
app中的一个TableView,使用原生的UILocalizedIndexedCollation进行索引排序(A-Z),实测速度很慢,400多条数据需要3秒多才能显示出来
定位后发现,瓶颈不在数据库访问和UI渲染上,就是索引排序太慢。优化前有性能问题的代码如下:
// slow method-(void) assembleMembers:(NSArray*)origin{ [members removeAllObjects]; UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; // slow point 1: takes 1.5 seconds when 400 records for (Member *member in origin) { NSInteger sect = [collation sectionForObject:member collationStringSelector:@selector(name)]; member.sectionNumber = sect; } NSInteger highSection = [[collation sectionTitles] count]; NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection]; for (int i = 0; i < highSection; i++) { NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1]; [sectionArrays addObject:sectionArray]; } for (Member *member in origin) { [(NSMutableArray*)[sectionArrays objectAtIndex:member.sectionNumber] addObject:member]; } // slow point 2: takes 1.3 seconds when 400 records for (NSMutableArray *sectionArray in sectionArrays) { NSArray *sortedSection = [collation sortedArrayFromArray:sectionArray collationStringSelector:@selector(name)]; [members addObject:sortedSection]; }}
有2段很慢,第一段是给Member分配sectionNumber,第二段是对26个子数组进行深度排序
发现了瓶颈,就针对瓶颈进行优化
分配sectionNumber的逻辑,放到每次插入数据库时。在表中增加section_number字段,保存这个值。这样每次从数据库取到的数据,就不需要在运行时排序了
深度排序耗时长的问题,改成首字母排序,速度也快了很多
最后的代码如下:
-(void) assembleMembers:(NSArray*)origin{ [members removeAllObjects]; UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; NSInteger highSection = [[collation sectionTitles] count]; NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection]; for (int i = 0; i < highSection; i++) { NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1]; [sectionArrays addObject:sectionArray]; } for (Member *member in origin) { [(NSMutableArray*)[sectionArrays objectAtIndex:member.sectionNumber] addObject:member]; } for (NSMutableArray *sectionArray in sectionArrays) { [sectionArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { Member *m1 = (Member*) obj1; Member *m2 = (Member*) obj2; return [m2.name localizedCompare:m1.name]; }]; [members addObject:sectionArray]; }}
这样改完之后,400条数据的处理时间缩短到0.3秒,基本可以接受了
0 0
- 优化TableView索引排序的性能
- tableView的性能优化
- tableView的性能优化
- iOS TableView的性能优化
- 关于tableView的性能优化
- TableView在iOS上的性能优化
- (十七)TableView的本地性能优化
- TableView在iOS上的性能优化
- TableView 性能优化
- TableView 性能优化
- tableView cell性能优化
- tableView性能优化
- TableView性能优化
- TableView性能优化
- tableView 性能优化机制
- iOS性能优化-TableView
- TableView性能优化
- tableView性能优化
- 自己对编程感悟
- 204 Coin Test
- 视频前景提取 (I)【滑动条版本】
- javaWeb-day01(junit 测试工具、XML)
- 206 矩形的个数
- 优化TableView索引排序的性能
- HDOJ 题目2089 不要62 (打表 水题)
- winio
- 214 单调递增子序列(二)
- Myeclipse如何自动创建hibernate,将工程转变为hibernate工程
- hdu2019 数列有序!
- poj-3267
- 插入排序和希尔排序
- ios 相关