mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-09-14 14:41:28 +00:00
Improved CPArray sortUsingDescriptors performance by about 3% by pulling the descriptor count out of the sort function.
This commit is contained in:
committed by
Ross Boucher
parent
fddcde757f
commit
791e65c88b
@@ -490,10 +490,11 @@
|
||||
*/
|
||||
- (unsigned)indexOfObject:(id)anObject sortedByDescriptors:(CPArray)descriptors
|
||||
{
|
||||
var count = [descriptors count];
|
||||
|
||||
return [self indexOfObject:anObject sortedByFunction:function(lhs, rhs)
|
||||
{
|
||||
var i = 0,
|
||||
count = [descriptors count],
|
||||
result = CPOrderedSame;
|
||||
|
||||
while (i < count)
|
||||
@@ -971,21 +972,22 @@
|
||||
|
||||
- (unsigned)insertObject:(id)anObject inArraySortedByDescriptors:(CPArray)descriptors
|
||||
{
|
||||
var count = [descriptors count];
|
||||
|
||||
var index = [self _indexOfObject:anObject sortedByFunction:function(lhs, rhs)
|
||||
{
|
||||
var i = 0,
|
||||
count = [descriptors count],
|
||||
result = CPOrderedSame;
|
||||
|
||||
while (i < count)
|
||||
if ((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame)
|
||||
if((result = [descriptors[i++] compareObject:lhs withObject:rhs]) != CPOrderedSame)
|
||||
return result;
|
||||
|
||||
return result;
|
||||
} context:nil];
|
||||
|
||||
if (index < 0)
|
||||
index = -index - 1;
|
||||
index = -index-1;
|
||||
|
||||
[self insertObject:anObject atIndex:index];
|
||||
return index;
|
||||
@@ -1191,10 +1193,11 @@
|
||||
|
||||
- (CPArray)sortUsingDescriptors:(CPArray)descriptors
|
||||
{
|
||||
var count = [descriptors count];
|
||||
|
||||
sort(function(lhs, rhs)
|
||||
{
|
||||
var i = 0,
|
||||
count = [descriptors count],
|
||||
result = CPOrderedSame;
|
||||
|
||||
while (i < count)
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
@import <Foundation/CPArray.j>
|
||||
@import <Foundation/CPString.j>
|
||||
@import <Foundation/CPNumber.j>
|
||||
@import <Foundation/CPSortDescriptor.j>
|
||||
|
||||
@implementation CPArrayPerformance : OJTestCase
|
||||
|
||||
- (void)testSortUsingDescriptorsSpeed
|
||||
{
|
||||
|
||||
var ELEMENTS = 1000,
|
||||
REPEATS = 10,
|
||||
array = [];
|
||||
for (var i=0; i<ELEMENTS; i++) {
|
||||
var s = [Sortable new];
|
||||
[s setA:(i % 5)];
|
||||
[s setB:(ELEMENTS-i)];
|
||||
array.push(s);
|
||||
}
|
||||
|
||||
var descriptors = [
|
||||
[CPSortDescriptor sortDescriptorWithKey:"a" ascending:NO],
|
||||
[CPSortDescriptor sortDescriptorWithKey:"b" ascending:YES]
|
||||
];
|
||||
|
||||
var start = (new Date).getTime();
|
||||
|
||||
for (var i=0; i<REPEATS; i++)
|
||||
{
|
||||
var sorted = [array sortedArrayUsingDescriptors:descriptors];
|
||||
|
||||
// Verify it really got sorted.
|
||||
for (var j=0; j<ELEMENTS; j++) {
|
||||
var expectedA = 4-FLOOR(j * 5 / ELEMENTS);
|
||||
if (sorted[j].a != expectedA)
|
||||
[self fail:"a out of order: "+expectedA+" != "+sorted[j].a];
|
||||
var expectedB = (5-expectedA) + 5 * (j % (ELEMENTS / 5))
|
||||
if (sorted[j].b != expectedB)
|
||||
[self fail:"b out of order: "+expectedB+" != "+sorted[j].b];
|
||||
}
|
||||
}
|
||||
|
||||
var end = (new Date).getTime();
|
||||
|
||||
CPLog.warn("testSortUsingDescriptorsSpeed: "+(end-start)+"ms");
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation Sortable : CPObject
|
||||
{
|
||||
int a @accessors;
|
||||
int b @accessors;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user