gpt4 book ai didi

ios - 哪个更快? for 循环或 isEqualToArray

转载 作者:可可西里 更新时间:2023-11-01 05:36:07 25 4
gpt4 key购买 nike

我想知道 isEqualToArray 实际上做了什么......

我有一个大小为 160 的数组,每个数组包含一个包含 11 个条目的字典,但我可以简单地根据第一列(包含行更改的日期)进行比较。

现在我可以用一个简单的 for-cycle 来做到这一点:

        BOOL different = FALSE;
for (int index = 0 ; index < [newInfo count] ; ++index)
if (![[[oldInfo objectAtIndex:index] objectForKey:@"Update"] isEqual:[[newInfo objectAtIndex:index] objectForKey:@"Update"]]) {

different = TRUE;
break;
}
if (different) {
}
else
NSLog(@"Contact information hasn't been updated yet");

或者我可以使用内置的 isEqualToArray 方法:

        if ([oldInfo isEqualToArray:newInfo])
NSLog(@"Contact information hasn't been updated yet");
else {
NSLog(@"Contact information has been updated, saving new contact information");
[newInfo writeToFile:path atomically:YES];
}

现在,如果假设 isEqualToArray 只是为每个单元调用 isEqualTo,则 for 循环方法运行时间为 isEqualToArray 的 1/11确实(只需要比较一列而不是 11 列)。

也许我太过专注于优化......(我参加过许多运行时间有限的比赛,我感受到了后遗症)。

最佳答案

The Documentation说:

Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.

所以基本上你是对的。

从设计的角度来看,我会选择 isEqualToArray:,因为它使代码更容易理解,或者如果您担心性能,则引入 BOOL hasUpdates ,它还有一个额外的优势,即您不必在内存中保留两个副本。

关于ios - 哪个更快? for 循环或 isEqualToArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18443124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com