NSSet Iteration consumes more time than NSMutableArray Iteration and NSArray Iteration consumes more time than NSMutableArray Iteration

Although I am looking through the following NSSet , NSMutableArray , NSFastEnumeration , I cannot find a satisfactory source for the scenario below:

Here NSMutableArray, NSArrayand NSSetcontain the same 10,000,000 objects.

for (NSString *strIn in MutableArray) //NSMutableArray
{
    // same Implementation
}
NSLog(@"Time for Mutable Array  %d Iteration : %f",ObjectCount,[[NSDate date]timeIntervalSinceDate:startDate]);
startDate = [NSDate date];
for (NSString *strIn in array)  //NSArray
{
    // same Implementation
}
NSLog(@"Time for  NSArray  %d Iteration : %f",ObjectCount,[[NSDate date]timeIntervalSinceDate:startDate]);
startDate = [NSDate date];
for (NSString *strIn in Set) //NSSet
{
    // same Implementation
}
NSLog(@"Time for Set  %d Iteration : %f",ObjectCount,[[NSDate date]timeIntervalSinceDate:startDate]);

The output is as follows:

Time for NSMutableArray10,000,000 Iteration: 0.048785

Time for NSArray10,000,000 Iteration: 0.390537

Time for NSSet10,000,000 Iteration: 4.684203

Why is there such a huge difference between iteration NSSetand NSArraytime.

, .

: - Set. . , , , . , .

+4
2

:

#import <Foundation/Foundation.h>

#define COLLECTION_SIZE 10000000

static NSString *randomString() {
    unichar buffer[18];
    NSUInteger size = (arc4random() % 12) + 6;
    for (NSUInteger i = 0; i < size; i++) {
        buffer[i] = (arc4random() % 93) + '!';
    }
    return [[NSString alloc] initWithCharacters:buffer length:size];
}

static NSSet *createCollection(NSUInteger size) {
    NSMutableSet *collection = [[NSMutableSet alloc] init];
    for (NSUInteger i = 0; i < size; i++) {
        for (;;) {
            NSString *s = randomString();
            if (![collection member:s]) {
                [collection addObject:s];
                break;
            }
        }
    }
    return collection;
}

static NSTimeInterval timedIter(id<NSFastEnumeration> collection) {
    NSUInteger totalLength = 0;
    NSDate *startDate = [NSDate date];
    for (NSString *s in collection) {
        totalLength += [s length];
    }
    return [[NSDate date] timeIntervalSinceDate:startDate];
}

int main(int argc, const char **argv) {
    @autoreleasepool {
        NSSet *set = createCollection(COLLECTION_SIZE);
        NSArray *array = [set allObjects];
        NSMutableArray *mutArray = [[set allObjects] mutableCopy];

        NSLog(@"set iteration=%f", timedIter(set));
        NSLog(@"array iteration=%f", timedIter(array));
        NSLog(@"mutArray iteration=%f", timedIter(mutArray));
    }
    return 0;
}

$ clang -o itertime itertime.m -fobjc-arc -framework Foundation
$ ./itertime
2013-11-13 11:23:13.344 itertime[77576:707] set iteration=0.422592
2013-11-13 11:23:13.654 itertime[77576:707] array iteration=0.309387
2013-11-13 11:23:13.964 itertime[77576:707] mutArray iteration=0.309107
+3

- . , , . 10M NSNumbers, :

Time for __NSArrayM: 0.334106
Time for __NSArrayI: 0.335368
Time for __NSSetI: 0.373651

, , .

+2

All Articles