守着一只汪
一些基准1. reverseObjectEnumerator allObjects这是最快的方法:NSArray *anArray = @[@"aa", @"ab", @"ac", @"ad", @"ae", @"af", @"ag",
@"ah", @"ai", @"aj", @"ak", @"al", @"am", @"an", @"ao", @"ap", @"aq", @"ar", @"as", @"at",
@"au", @"av", @"aw", @"ax", @"ay", @"az", @"ba", @"bb", @"bc", @"bd", @"bf", @"bg", @"bh",
@"bi", @"bj", @"bk", @"bl", @"bm", @"bn", @"bo", @"bp", @"bq", @"br", @"bs", @"bt", @"bu",
@"bv", @"bw", @"bx", @"by", @"bz", @"ca", @"cb", @"cc", @"cd", @"ce", @"cf", @"cg", @"ch",
@"ci", @"cj", @"ck", @"cl", @"cm", @"cn", @"co", @"cp", @"cq", @"cr", @"cs", @"ct", @"cu",
@"cv", @"cw", @"cx", @"cy", @"cz"];NSDate *methodStart = [NSDate date];NSArray *reversed = [[anArray reverseObjectEnumerator] allObjects];NSDate *methodFinish = [NSDate date];NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];NSLog(@"executionTime = %f", executionTime);结果: executionTime = 0.0000262.迭代reverseObjectEnumerator这速度在1.5x到2.5x之间:NSDate *methodStart = [NSDate date];NSMutableArray *array = [NSMutableArray arrayWithCapacity:[anArray count]];NSEnumerator *enumerator = [anArray reverseObjectEnumerator];for (id element in enumerator) {
[array addObject:element];}NSDate *methodFinish = [NSDate date];NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];NSLog(@"executionTime = %f", executionTime);结果: executionTime = 0.0000713. sortedArrayUsingComparator速度在30x到40x之间(这里没有惊喜):NSDate *methodStart = [NSDate date];NSArray *reversed = [anArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
return [anArray indexOfObject:obj1] < [anArray indexOfObject:obj2] ? NSOrderedDescending : NSOrderedAscending;}];NSDate *methodFinish = [NSDate date];NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];NSLog(@"executionTime = %f", executionTime);结果: executionTime = 0.001100[[anArray reverseObjectEnumerator] allObjects]在速度和轻松方面,明显的赢家也是如此。