Objective:
For iOS project development always meet a variety of collection traversal, out of curiosity about the various traversal efficiency, so prepare to write a test program test
First: Declare a nsmutablearray, the amount of test data is 1000, 10,000, 100,000.
int Testmaxcount =+; // 10000,100000 *testarray= for (int i =0; i<testmaxcount; i++) {[ Testarray addobject:@ "test"];}
First type: normal for loop
// normal for Loop = cfabsolutetimegetcurrent (); for (int i =0; I<[testarray count];i++) { NSLog (@ "%@") , Testarray[i]); } = cfabsolutetimegetcurrent (); NSLog (@ " normal for loop time cost:%0.3f", End-start);
Second type: for In loop
// For in Loop Start = cfabsolutetimegetcurrent (); for inch Testarray) { NSLog (@ "%@", TempStr); } = cfabsolutetimegetcurrent (); NSLog (@ " for In loop time cost :%0.3f", End-start);
Third Type: code block loop
// code block Start = cfabsolutetimegetcurrent (); [Testarray enumerateobjectsusingblock:^ (id _nonnull obj, Nsuinteger idx, BOOL * _nonnull stop) { C8/>nslog (@ "%@", obj); }]; = cfabsolutetimegetcurrent (); NSLog (@ " code block cycle time Cost:%0.3f", End-start);
Fourth Type: Enumerator loop
// Enumerator Start = cfabsolutetimegetcurrent (); *enumerator=[Testarray objectenumerator]; while (enumerator.nextobject) { NSLog (@ "%@", Enumerator.nextobject ); } = cfabsolutetimegetcurrent (); NSLog (@ " Enumerator cycle time cost:%0.3f", End-start);
Execution Result:
1. Test Data 1000 Article 10,000 article 100,000 article
Normal for loop:0.391 2.390 18.400
For-in cycle:0.226 2.782 15.172
code block loop:0.241 2.744 15.123
Enumerator loop:0.147 1.429 7.432
Conclusion: The most rapid traversal is that the enumerator traverses the other three kinds of traversal efficiency.
Object-c Comparison of various collection traversal efficiency