Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Foundation Collection Classes

Foundation Collection Classes

Talk give at the AltTechTalks Berlin (http://alt-tech-talks.com) that summarizes my talk in objc.io Issue 7: http://objc.io/issue-7/collections.html

Peter Steinberger

December 11, 2013
Tweet

More Decks by Peter Steinberger

Other Decks in Technology

Transcript

  1. Performance Object Access usual: O(1) worst case: O(lg N) Insertion/Deletion

    usual: O(N) worst case: O(N*lg N) Linear Search O(N*lg N)
  2. NSArray *array = @[@"John Appleseed", @"Tim Cook", @"Hair Force One",

    @"Michael Jurewitz”]; ! NSArray *sortedArray = [array sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)];
  3. NSArray *numbers = @[@9, @5, @11, @3, @1]; ! NSArray

    *sortedNumbers = [numbers sortedArrayUsingSelector:@selector(compare:)];
  4. typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) { NSBinarySearchingFirstEqual = (1UL << 8), NSBinarySearchingLastEqual

    = (1UL << 9), NSBinarySearchingInsertionIndex = (1UL << 10), }; ! - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp;
  5. typedef NS_OPTIONS(NSUInteger, NSBinarySearchingOptions) { NSBinarySearchingFirstEqual = (1UL << 8), NSBinarySearchingLastEqual

    = (1UL << 9), NSBinarySearchingInsertionIndex = (1UL << 10), }; ! - (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp;
  6. NSMutableArray *mutableArray = [NSMutableArray array]; for (NSUInteger idx = 0;

    idx < randomArray.count; idx++) { id obj = randomArray[idx]; if (testObj(obj)) { [mutableArray addObject:obj]; } }
  7. NSMutableArray *mutableArray = [NSMutableArray array]; NSEnumerator *enumerator = [randomArray objectEnumerator];

    id obj = nil; while ((obj = [enumerator nextObject]) != nil) { if (testObj(obj)) { [mutableArray addObject:obj]; } }
  8. NSMutableArray *mutableArray = [NSMutableArray array]; for (id obj in randomArray)

    { if (testObj(obj)) { [mutableArray addObject:obj]; } }
  9. NSIndexSet *indexes = [randomArray indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL

    *stop) { return testObj(obj); }]; __unused NSArray *filteredArray1 = [randomArray objectsAtIndexes:indexes];
  10. indexesOfObjects: (concurrent) 1844.73 [ms] NSFastEnumeration 3223.45 [ms] indexesOfObjects: 4221.23 [ms]

    objectAtIndex: 5282.67 [ms] enumerateObjectsUsingBlock: 5459.43 [ms] NSEnumeration 5566.92 [ms] filteredArrayUsingPredicate: 6466.95 [ms] Enumerating + Filtering 10.000.000 elements
  11. NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary]; for (id key in randomDict)

    { id obj = randomDict[key]; if (testObj(obj)) { mutableDictionary[key] = obj; } }
  12. NSArray *keys = [randomDict keysOfEntriesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id key, id obj, BOOL

    *stop) { return testObj(obj); }].allObjects; ! NSDictionary *filteredDictionary = [NSDictionary dictionaryWithObjects:[randomDict objectsForKeys:keys notFoundMarker:NSNull.null] forKeys:keys];
  13. NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary]; id __unsafe_unretained *objects = (id

    __unsafe_unretained *)malloc(sizeof(id) * entries); id __unsafe_unretained *keys = (id __unsafe_unretained *)(malloc(sizeof(id) * entries)); [randomDict getObjects:objects andKeys:keys]; for (int i = 0; i < entries; i++) { id obj = objects[i]; id key = keys[i]; if (testObj(obj)) { mutableDictionary[key] = obj; } } free(objects); free(keys);
  14. keysOfEntriesWithOptions:(concurrent) 425.24 [ms] getObjects:andKeys: 798.49 [ms] keysOfEntriesWithOptions: 856.93 [ms] enumerateKeysAndObjectsUsingBlock:

    882.93 [ms] NSFastEnumeration 1043.42 [ms] NSEnumeration 1113.08 [ms] Enumerating + Filtering 1.000.000 elements
  15. id sharedKeySet = [NSDictionary sharedKeySetForKeys:@[@1, @2, @3]]; // NSSharedKeySet NSMutableDictionary

    *test = [NSMutableDictionary dictionaryWithSharedKeySet:sharedKeySet]; NSDictionary *iAmStillAMutableObject = [test copy]; test[@4] = @"Works";
  16. NSMutableOrderedSet (adding) 3190.52 [ms] NSMutableSet (adding) 2511.96 [ms] NSMutableArray (adding)

    1423.26 [ms] NSMutableOrderedSet (random access) 10.74 [ms] NSMutableSet (random access) 4.47 [ms] NSMutableArray (random access) 8.08 [ms] Adding + Random Accessing 1.000.000 elements