One efficiency:
For VS for (...)
- For a wide range of applications such as Nsarray, Nsarray, and C-language arrays, and for (...) Limited to Nsarray, Nsarray, etc.
- For (...) more concise, more efficient
Test code:
10^7 Array, time unit seconds, precision milliseconds
- Nsmutablearray *test = [nsmutablearray array];
- For (int i= 0; i < 10000000; i++) {
- [Test addobject:@ (i)];
- }
- int sum = 0;
- Double date_s = cfabsolutetimegetcurrent ();
- For (int i = 0;i < test. Count; i++) {
- Sum + = 1;
- }
- Double date_e = cfabsolutetimegetcurrent ();
- NSLog (@ "Forloop time:%f", date_e-date_s);
- date_s = Cfabsolutetimegetcurrent ();
- For (ID obj in test) {
- Sum + = 1;
- }
- Date_e = Cfabsolutetimegetcurrent ();
- NSLog (@ "Enumeration time:%f", date_e-date_s);
Test results:
Taking into account the actual situation, forloop operation more.
Two The difference that is used For LoopIt is mainly used when you already know the number of cycles or the number of loops you can explicitly get, such as if you want to make 100 "Hello word!" on the screen or loop through the entire contents of an array, then you can use for (int i=0;i<100,i++) for (int i = 0; i < Array.count; i + +) Here you have made it very clear that the number of arrays that are 100 or get arrayFor in LoopIt is convenient to traverse an array that already knows the object type and does not care about the index, such as knowing that the array is UIButton so you can use for UIButton the *tmpbutton in Array so you can directly access each button in the array directly through the Tmpbutton the other array also has a block traversal method
1 [array enumerateobjectsusingblock:^ (ID obj, Nsuinteger idx, BOOL *stop) {2 //..... 3 }];
One thing to note about this approach is that there may be problems with asynchronous execution. The last three kinds of traversal methods are for In is the highest efficiency--------------------------------------------------------------------------------------------for in Loop traversal is a bit more advanced than for a, and when you have an array of elements that are the same, you can iterate with for in, and accept the same type of object that is stored in the array.
three things to note
If the array is modified in the for-in loop, either increment, delete, or modify the position of the array element, it throws an exception, and the error is that the array being traversed has been destroyed (<__NSArrayM:0xa4fc000> was mutated While being enumerated. ), such as the following code:
Nsmutablearray* arr = [Nsmutablearray arraywithobjects:@ "1",@ "2",@ "3", Nil];
for (nsstring* str in arr) {
if ([str isequaltostring:@ "1"] | | [Str isequaltostring:@ "5"]) {
[Arr addobject:@ "4"];// [arr removeobject:@ "1"]; or [arr exchangeobjectatindex: 0 withobjectatindex:2];
continue;
}
}
Reason:
For in is actually a quick enumeration, which is different from the for loop.
One of the Nsarray enumeration operations needs to be noted: when enumerating through mutable arrays, you cannot alter the array container by adding or removing objects like this. If you do this, the enumerator will be confused and you will get undefined results.
There is a problem with the operation itself, the array container has changed, it may traverse to a location that is not assigned, and the For loop machine cannot detect it by itself, but the enumerator can detect it.
Append, this error (<__NSArrayM:0xa4fc000> was mutated while being enumerated. ) means that the array has mutated in the process of the enumeration
IOS for and Forin differences and considerations