From 791e65c88b838e78bb29d132cf3e73dee2ceeb27 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 30 Apr 2010 15:11:28 -0400 Subject: [PATCH] Improved CPArray sortUsingDescriptors performance by about 3% by pulling the descriptor count out of the sort function. --- Foundation/CPArray.j | 13 ++++--- Tests/Foundation/CPArrayPerformance.j | 56 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 Tests/Foundation/CPArrayPerformance.j diff --git a/Foundation/CPArray.j b/Foundation/CPArray.j index e26731db4..4e22b591a 100755 --- a/Foundation/CPArray.j +++ b/Foundation/CPArray.j @@ -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) diff --git a/Tests/Foundation/CPArrayPerformance.j b/Tests/Foundation/CPArrayPerformance.j new file mode 100644 index 000000000..a41c16dce --- /dev/null +++ b/Tests/Foundation/CPArrayPerformance.j @@ -0,0 +1,56 @@ +@import +@import +@import +@import + +@implementation CPArrayPerformance : OJTestCase + +- (void)testSortUsingDescriptorsSpeed +{ + + var ELEMENTS = 1000, + REPEATS = 10, + array = []; + for (var i=0; i