什么是更快?枚举VS For循环 [英] What is faster? Enumeration VS For loop
本文介绍了什么是更快?枚举VS For循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标C和iphone的速度更快?自我枚举或for循环?
What is faster in objective C and iphone? self enumeration or for loop?
我有两个代码片段,可以帮助我比较。
i have 2 fragments of code to help me compare.
我们有一个事实,数组是一个NSMutableArray与x项目。
案例1:
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;
}
}
案例2:
-(void)findItem:(Item*)item
{
for(Item *temp in array)
{
if(item.tag == temp.tag)
return;
}
}
很明显case2更快, ?
it is almost obvious that case2 is faster, is it?
推荐答案
这称为快速枚举。
请参阅: http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html
这篇关于什么是更快?枚举VS For循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文