在开发应用程序的时候,有时我们需要对一组无序的内容进行排序,iOS中有系统自带的方法来对NSAray进行排序,我们来对这些方法进行性能上的对比:
NSComparator排序
NSDescriptor排序
function排序
quickSort排序
由于排序的对象经常是自定义的,因此我们定义一个如下的对象:
@interface Topic : NSObject @property (nonatomic, assign) NSInteger ID; @property (nonatomic, copy) NSString *content; @end
然后生成一个包含10000个对象的数组,对像的ID都是随机的:
NSMutableArray *unSortedArray = [NSMutableArray new]; for(NSInteger i = 0; i <10000;i++) { Topic *topic = [Topic new]; topic.ID = arc4random() % 10000; topic.content = [NSString stringWithFormat:@"This is :%@",[NSNumber numberWithLong: topic.ID]]; [unSortedArray addObject:topic]; }
计算时间差的方法:
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(); CFAbsoluteTime end = CFAbsoluteTimeGetCurrent(); NSLog(@"time cost: %0.3f ms", (end - start)*1000);
使用NSComparator排序
comparator的定义如下所示:
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
上面的参数(obj1、obj2)就是我们将要做比较的对象。block返回的结果为NSComparisonResult类型来表示两个对象的顺序。
对上述的无序array的对象ID进行排序,代码如下:
NSArray *sortedArray = [unSortedArray sortedArrayUsingComparator:^(id obj1,id obj2)
{
NSInteger val1 = ((Topic*)obj1).ID;
NSInteger val2 = ((Topic*)obj2).ID;
//升序,假如需要降序的话,只需要修改下面的逻辑
if (val1 < val2)
{
return NSOrderedAscending;
}
else
{
return NSOrderedDescending;
}
}
使用NSDescriptor排序
sort descriptor可以很方便的对数组进行多个key的排序。比如要对数组的对象先做ID排序,然后在对content进行排序的话,可以写成:
NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES]; NSSortDescriptor *secondDescriptor = [[NSSortDescriptor alloc] initWithKey:@"content" ascending:YES]; NSArray *sortArray = [NSArray arrayWithObjects:firstDescriptor,secondDescriptor,nil]; NSArray *sortedArray = [unSortedArray sortedArrayUsingDescriptors:sortArray];
使用函数排序
具体代码实现方式如下:
NSInteger customSort(id obj1, id obj2,void* context)
{
Topic *topic1 = (Topic*)obj1; Topic *topic2 = (Topic*)obj2; NSInteger val1 = topic1.ID; NSInteger val2 = topic2.ID; if (val1 > val2)
{ return (NSComparisonResult)NSOrderedDescending; } if (val1 < val2)
{ return (NSComparisonResult)NSOrderedAscending; } return (NSComparisonResult)NSOrderedSame; } sortedArray = [array sortedArrayUsingFunction:customSort context:nil];
快速排序
快速排序我想大多数的人都听过,由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此我们也对比以一下快排的表现,下面是快排的代码:
void quickSort(NSMutableArray *array, NSInteger first, NSInteger last, NSComparator comparator)
{ if (first >= last) return; id pivot = array[(first + last) / 2]; NSInteger left = first; NSInteger right = last; while (left <= right) { while (comparator(array[left], pivot) == NSOrderedAscending) left++; while (comparator(array[right], pivot) == NSOrderedDescending) right--; if (left <= right) [array exchangeObjectAtIndex:left++ withObjectAtIndex:right--]; } quickSort(array, first, right, comparator); quickSort(array, left, last, comparator); } NSArray* sort(NSArray *unsorted, NSComparator comparator) { NSMutableArray *a = [NSMutableArray arrayWithArray:unsorted]; quickSort(a, 0, a.count - 1, comparator);return a; } sortedArray = sort(array, ^(id obj1, id obj2) { Topic *topic1 = (Topic*)obj1; Topic *topic2 = (Topic*)obj2; NSNumber *val1 =[NSNumber numberWithLong:topic1.ID]; NSNumber *val2 = [NSNumber numberWithLong:topic2.ID]; return [val1 compare:val2]; });
结果对比:
iPhone4: 2014-10-17 13:51:31.980 Algorithm_test[9578:907] NSComparator sort time cost: 163.708ms 2014-10-17 13:51:32.273 Algorithm_test[9578:907] NSSortDescriptor sort time cost: 291.293ms 2014-10-17 13:51:32.559 Algorithm_test[9578:907] function sort time cost: 281.485ms 2014-10-17 13:51:36.582 Algorithm_test[9578:907] quick sort time cost: 4013.582ms
iPhone5s: 2014-10-17 14:02:59.323 Algorithm_test[2971:60b] NSComparator sort time cost: 19.238ms 2014-10-17 14:02:59.348 Algorithm_test[2971:60b] NSSortDescriptor sort time cost: 24.183ms 2014-10-17 14:02:59.380 Algorithm_test[2971:60b] function sort time cost: 31.967ms 2014-10-17 14:02:59.468 Algorithm_test[2971:60b] quick sort time cost: 86.205ms
可以发现前3种系统自带的方法运行速度很快,即便是在4这种老机器排序10000个对象也不到1s的时间,可以看出苹果对算法的优化还是挺好的,但是快排的表现却不尽如人意,至于5s机器上,上述的排序时间都在几十毫秒,几乎可以忽略不计。因此建议在需要排序的时候采用系统自带的方法,至于用哪个可以看情况自己选择。
示例代码:https://github.com/FreeMind-LJ/HelloWrold/tree/master/Algorithm_test