When developing applications, sometimes we need to sort out a set of unordered content, and iOS has a way to sort the nsaray in the system, and we'll compare these methods in performance:
Nscomparator sort
Nsdescriptor sort
function sort
Quicksort sort
Because the sorted objects are often custom, we define an object as follows:
@interface*content; @end
It then generates an array of 10,000 objects, with the ID of the image random:
New ]; for 0; I <10000; i++) { new]; 10000 ; = [NSString stringWithFormat:@ "This is:%@", [NSNumber Numberwithlong: Topic.id]]; [Unsortedarray addobject:topic];}
How to calculate the time difference:
Cfabsolutetime start = cfabsolutetimegetcurrent (); Cfabsolutetime end = cfabsolutetimegetcurrent (); NSLog (@ "time cost:%0.3f ms", (end-start) *+);
Sorting using Nscomparator
The definition of comparator is as follows:
typedef Nscomparisonresult (^nscomparator) (ID obj1, id obj2);
The above parameters (Obj1, obj2) are the objects we will be comparing. The block returns the result of the Nscomparisonresult type to represent the order of two objects.
To sort the object IDs of the unordered array above, the code is as follows:
Nsarray *sortedarray = [Unsortedarray sortedarrayusingcomparator:^ (ID obj1,ID obj2)
{
= ((topic*) obj1). ID;
= ((topic*) obj2). ID;
// In ascending order, if you need to do this, you only need to modify the following logic
if (Val1 < val2)
{
return nsorderedascending;
}
Else
{
return nsordereddescending;
}
}
Sorting using Nsdescriptor
Sort descriptor can be handy for sorting multiple keys in an array. For example, to sort the objects of an array first, and then sort the content, you can write:
Nssortdescriptor *firstdescriptor = [[Nssortdescriptor alloc] Initwithkey:@ "ID" *seconddescriptor = [[Nssortdescriptor alloc] Initwithkey:@ "content"* Sortarray = [Nsarray arraywithobjects:firstdescriptor,seconddescriptor,nil]; Nsarray *sortedarray = [Unsortedarray Sortedarrayusingdescriptors:sortarray];
Sorting using Functions
The specific code is implemented as follows:
Nsinteger Customsort (IDObj1,IDObj2,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];
Quick Sort
Quick sort I think most people have heard that because the sorting efficiency is more efficient in several sorting methods of the same O (N*logn), we also compare the performance of the quick row, the following is the code of the Fast line:
voidQuickSort (Nsmutablearray *Array, Nsinteger first, Nsinteger last, Nscomparator comparator)
{ if(First >= last)return; IDPivot = 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);returnA;} Sortedarray= Sort (array, ^ (IDObj1,IDobj2) {Topic*topic1 = (topic*) obj1; Topic*topic2 = (topic*) Obj2; NSNumber*val1 =[NSNumber NumberWithLong:topic1.ID]; NSNumber*val2 =[NSNumber NumberWithLong:topic2.ID]; return[Val1 Compare:val2]; });
Results Comparison :
iPhone4: the-Ten- - -:Wuyi:31.980algorithm_test[9578:907] Nscomparator sort Time Cost:163. 708ms the-Ten- - -:Wuyi:32.273algorithm_test[9578:907] Nssortdescriptor sort Time Cost:291. 293ms the-Ten- - -:Wuyi:32.559algorithm_test[9578:907] Function sort Time Cost:281. 485ms the-Ten- - -:Wuyi:36.582algorithm_test[9578:907] Quick sort Time Cost:4013. 582ms
IPhone5S: the-Ten- - -: Geneva:59.323algorithm_test[2971: 60b] Nscomparator sort time Cost: +. 238ms the-Ten- - -: Geneva:59.348algorithm_test[2971: 60b] Nssortdescriptor sort time Cost: -. 183ms the-Ten- - -: Geneva:59.380algorithm_test[2971: 60b] Function sort time Cost: to. 967ms the-Ten- - -: Geneva:59.468algorithm_test[2971: 60b] Quick sort time Cost: the.205ms
Can find the first 3 kinds of system comes with the method of running fast, even in 4 this old machine sort 10,000 objects also less than 1s of time, you can see Apple optimization of the algorithm is still very good, but the performance of the fast row is not satisfactory, as for the 5s machine, the above sorting time in dozens of milliseconds, can be almost negligible. Therefore, it is recommended to use the system's own method when ordering, as to which one can see the situation.
Example code:https://github.com/FreeMind-LJ/HelloWrold/tree/master/Algorithm_test
Comparison of sequencing methods for iOS development