NSEnumerator performance vs for loop in Cocoa

前端 未结 3 660
眼角桃花
眼角桃花 2020-12-18 19:27

I know that if you have a loop that modifies the count of the items in the loop, using the NSEnumerator on a set is the best way to make sure your code blows up, however I w

3条回答
  •  Happy的楠姐
    2020-12-18 20:24

    After running the test several times, the result is almost the same. Each measure block runs 10 times consecutively.

    The result in my case from the fastest to the slowest:

    1. For..in (testPerformanceExample3) (0.006 sec)
    2. While (testPerformanceExample4) (0.026 sec)
    3. For(;;) (testPerformanceExample1) (0.027 sec)
    4. Enumeration block (testPerformanceExample2) (0.067 sec)

    The for and while loop is almost the same.

    comparation between iterations

    The tmp is an NSArray which contains 1 million objects from 0 to 999999.

    - (NSArray *)createArray
    {
        self.tmpArray = [NSMutableArray array];
        for (int i = 0; i < 1000000; i++)
        {
            [self.tmpArray addObject:@(i)];
        }
        return self.tmpArray;
    }
    

    The whole code:

    ViewController.h

    #import 
    
    @interface ViewController : UIViewController
    
    @property (strong, nonatomic) NSMutableArray *tmpArray;
    - (NSArray *)createArray;
    
    @end
    

    ViewController.m

    #import "ViewController.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self createArray];
    }
    
    - (NSArray *)createArray
    {
        self.tmpArray = [NSMutableArray array];
        for (int i = 0; i < 1000000; i++)
        {
            [self.tmpArray addObject:@(i)];
        }
        return self.tmpArray;
    }
    
    @end
    

    MyTestfile.m

    #import 
    #import 
    
    #import "ViewController.h"
    
    @interface TestCaseXcodeTests : XCTestCase
    {
        ViewController *vc;
        NSArray *tmp;
    }
    
    @end
    
    @implementation TestCaseXcodeTests
    
    - (void)setUp {
        [super setUp];
        vc = [[ViewController alloc] init];
        tmp = vc.createArray;
    }
    
    - (void)testPerformanceExample1
    {
        [self measureBlock:^{
            for (int i = 0; i < [tmp count]; i++)
            {
                [tmp objectAtIndex:i];
            }
        }];
    }
    
    - (void)testPerformanceExample2
    {
        [self measureBlock:^{
            [tmp enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {
               obj;
            }];
        }];
    }
    
    - (void)testPerformanceExample3
    {
        [self measureBlock:^{
            for (NSNumber *num in tmp)
            {
                num;
            }
        }];
    }
    
    - (void)testPerformanceExample4
    {
        [self measureBlock:^{
            int i = 0;
            while (i < [tmp count])
            {
                [tmp objectAtIndex:i];
                i++;
            }
        }];
    }
    
    @end
    

    For more information visit: Apples "About Testing with Xcode"

提交回复
热议问题