Object-c Comparison of various collection traversal efficiency

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.