ブロックの繰り返しは列挙よりも速いと思いましたが、場合によってはそうです。ただし、データ配列があり、さまざまな反復アプローチを使用して複数の配列を作成しているこの単純な例では、結果は期待したものではありません。
説明があれば助かります。
NSMutableArray *dataArray =[[[NSMutableArray alloc] init] autorelease];
NSMutableArray *mArray1 = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *mArray2 = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *mArray3 = [[[NSMutableArray alloc] init] autorelease];
NSDate *dt1 = [NSDate date];
for (int j=0; j<10000000;j++)
{
[dataArray addObject:[NSNumber numberWithInt:j]];
}
NSDate *dt2 = [NSDate date];
int cnt = [dataArray count];
//Using normal for loop
for (int k=0; k<cnt;k++)
{
[mArray1 addObject:[dataArray objectAtIndex:k]];
}
//Using Fast Enumeration
NSDate *dt3 = [NSDate date];
for (NSNumber *num in dataArray)
{
[mArray2 addObject:num];
}
//Enumerating using Blocks
NSDate *dt4 = [NSDate date];
[dataArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mArray3 addObject:obj];
}];
NSDate *dt5 = [NSDate date];
NSLog(@"Time taken to create the data array %f",[dt2 timeIntervalSinceDate:dt1]);
NSLog(@"Time taken to iterate using normal for loop %f",[dt3 timeIntervalSinceDate:dt2]);
NSLog(@"Time taken to iterate using fast enumeration %f",[dt4 timeIntervalSinceDate:dt3]);
NSLog(@"Time taken to iterate using blocks %f",[dt5 timeIntervalSinceDate:dt4]);
//データ配列の作成にかかった時間 0.383750
// 通常のループ 0.309719 を使用して反復するのにかかった時間
//高速列挙を使用して反復するのにかかった時間 0.278467
//ブロックを使用して反復するのにかかった時間 0.526629