Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is faster? Enumeration VS For loop

What is faster in objective C and iphone? self enumeration or for loop?

i have 2 fragments of code to help me compare.

for this example we have as a fact that array is an NSMutableArray with "x" items. Case 1:

-(void)findItem:(Item*)item
{
  Item *temp;
  for (int i = 0 ;i<[array count];i++)
  {

    temp = [array objectAtIndex:i];
    if(item.tag == temp.tag)
      return;
  }


}

Case 2:

-(void)findItem:(Item*)item
{
  for(Item *temp in array)
  {
    if(item.tag == temp.tag)
      return;
  }
}

it is almost obvious that case2 is faster, is it?

like image 585
L_Sonic Avatar asked Jan 20 '23 07:01

L_Sonic


1 Answers

It's called fast enumeration, for a reason.

See: http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html

like image 149
petert Avatar answered Jan 25 '23 06:01

petert