隨着iOS的不斷髮展,apple也不斷推出性能更高的數組遍歷方式,下面將對熟悉的五種遍歷方式進行列舉。數組
首先定義一個數組,並獲取數組長度多線程
NSArray *array=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",]; NSInteger count =array.count;
1、for循環app
for (NSInteger i=0; i<count; i++) { NSLog(@"%@----%@",array[i],[NSThread currentThread]); } 打印結果以下: 2017-01-03 11:02:01.537 MyTestWorkProduct[2013:76489] 1----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.538 MyTestWorkProduct[2013:76489] 2----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 3----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 4----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.539 MyTestWorkProduct[2013:76489] 5----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 6----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 7----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.540 MyTestWorkProduct[2013:76489] 8----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.541 MyTestWorkProduct[2013:76489] 9----<NSThread: 0x600000260e80>{number = 1, name = main} 2017-01-03 11:02:01.541 MyTestWorkProduct[2013:76489] end
2、forin 快速枚舉性能
for (NSString *string in array) { NSLog(@"%@----%@",string,[NSThread currentThread]); } 打印結果以下: 2017-01-03 11:04:01.990 MyTestWorkProduct[2086:78292] 1----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.990 MyTestWorkProduct[2086:78292] 2----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.991 MyTestWorkProduct[2086:78292] 3----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.992 MyTestWorkProduct[2086:78292] 4----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.992 MyTestWorkProduct[2086:78292] 5----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 6----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 7----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.993 MyTestWorkProduct[2086:78292] 8----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.994 MyTestWorkProduct[2086:78292] 9----<NSThread: 0x60000007a400>{number = 1, name = main} 2017-01-03 11:04:01.994 MyTestWorkProduct[2086:78292] end
3、NSEnumeratorspa
NSEnumerator *enumer=[array objectEnumerator]; id obj; while (obj=[enumer nextObject]) { NSLog(@"%@----%@",obj,[NSThread currentThread]); } 打印結果以下: 2017-01-03 11:05:37.138 MyTestWorkProduct[2156:79788] 1----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.139 MyTestWorkProduct[2156:79788] 2----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 3----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 4----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.140 MyTestWorkProduct[2156:79788] 5----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 6----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 7----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.141 MyTestWorkProduct[2156:79788] 8----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.142 MyTestWorkProduct[2156:79788] 9----<NSThread: 0x608000078980>{number = 1, name = main} 2017-01-03 11:05:37.142 MyTestWorkProduct[2156:79788] end
4、快速遍歷線程
//順序遍歷 [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"%@----%@",array[idx],[NSThread currentThread]); }]; //倒序遍歷 [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"%@----%@",array[idx],[NSThread currentThread]); }]; 打印結果以下: 2017-01-03 11:07:30.774 MyTestWorkProduct[2229:81188] 1----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.775 MyTestWorkProduct[2229:81188] 2----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.775 MyTestWorkProduct[2229:81188] 3----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.776 MyTestWorkProduct[2229:81188] 4----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.777 MyTestWorkProduct[2229:81188] 5----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.778 MyTestWorkProduct[2229:81188] 6----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.778 MyTestWorkProduct[2229:81188] 7----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.779 MyTestWorkProduct[2229:81188] 8----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.779 MyTestWorkProduct[2229:81188] 9----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.780 MyTestWorkProduct[2229:81188] 9----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.781 MyTestWorkProduct[2229:81188] 8----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.781 MyTestWorkProduct[2229:81188] 7----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.782 MyTestWorkProduct[2229:81188] 6----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.782 MyTestWorkProduct[2229:81188] 5----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.783 MyTestWorkProduct[2229:81188] 4----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.784 MyTestWorkProduct[2229:81188] 3----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.785 MyTestWorkProduct[2229:81188] 2----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.786 MyTestWorkProduct[2229:81188] 1----<NSThread: 0x600000070240>{number = 1, name = main} 2017-01-03 11:07:30.786 MyTestWorkProduct[2229:81188] end
5、快速迭代code
//將block中的任務,逐個放到queue中,而後進行dispatch_sync執行 //多線程同步循環 dispatch_queue_t queue =dispatch_queue_create("apply並行隊列", DISPATCH_QUEUE_CONCURRENT); dispatch_apply(count, queue, ^(size_t index) { NSLog(@"%@----%@",array[index],[NSThread currentThread]); });
NSLog(@"end");blog
打印結果以下: 2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82847] 1----<NSThread: 0x60000006b940>{number = 1, name = main} 2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82977] 3----<NSThread: 0x600000876cc0>{number = 7, name = (null)} 2017-01-03 11:09:00.114 MyTestWorkProduct[2302:83008] 2----<NSThread: 0x600000479a80>{number = 6, name = (null)} 2017-01-03 11:09:00.114 MyTestWorkProduct[2302:83482] 4----<NSThread: 0x60800067e740>{number = 8, name = (null)} 2017-01-03 11:09:00.114 MyTestWorkProduct[2302:82847] 5----<NSThread: 0x60000006b940>{number = 1, name = main} 2017-01-03 11:09:00.115 MyTestWorkProduct[2302:82977] 6----<NSThread: 0x600000876cc0>{number = 7, name = (null)} 2017-01-03 11:09:00.115 MyTestWorkProduct[2302:83008] 7----<NSThread: 0x600000479a80>{number = 6, name = (null)} 2017-01-03 11:09:00.116 MyTestWorkProduct[2302:83482] 8----<NSThread: 0x60800067e740>{number = 8, name = (null)} 2017-01-03 11:09:00.116 MyTestWorkProduct[2302:82847] 9----<NSThread: 0x60000006b940>{number = 1, name = main} 2017-01-03 11:09:00.116 MyTestWorkProduct[2302:82847] end
根據打印結果最後纔打印 end 字符可知,dispatch_apply會等待每一個block中的任務都執行完成後,才往下執行。隊列
總結:從這五種遍歷方式來看,第5種採用的多線程,對於處理耗時的數組遍歷比較適用。同步