Is this an inefficient way of using fast enumeration?

前端 未结 2 1320

I don\'t entirely understand the details of how fast enumeration works, but compare the following two cases:

for(NSObject *object in self.myParent.parentsPar         


        
2条回答
  •  星月不相逢
    2021-01-13 13:40

    That's probably compiler-specific (i.e. undefined). If you are that bothered then add some timing code and find out yourself:

    #import 
    
    static unsigned getTickCount()
    {
        struct timeval tv;
        gettimeofday(&tv, 0);
        return (unsigned)((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
    }
    

    ...

    unsigned startTime = getTickCount();
    for(NSObject *object in self.myParent.parentsParents.granfathersMother.cousin.unclesNephew.array) {
        // do something
    }
    unsigned endTime = getTickCount();
    NSLog(@"That took %umS", endTime - startTime);
    

    You will have to have a pretty big array however in order to register anything above 0.

提交回复
热议问题