4

我想知道isEqualToArray实际上是什么...

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

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

        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];
        }

现在,如果假设isEqualToArrayisEqualTo为每个单元格调用,for-loop 方法运行时间的 1/11 isEqualToArray(只需要比较一列而不是 11)。

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

4

3 回答 3

4

文档说:

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.

So basically you are right.

From a design point of view I would either go for isEqualToArray:, since it makes the code easier to understand or introduce a BOOL hasUpdates if you are concern about performance, which has the additionally advantage that you don't have to hold two copies in memory.

于 2013-08-26T11:52:22.290 回答
2

I suspect that many people wrongly assume that performance is proportional to the number of source statements executed and that a function like isEqualToArray is blindingly fast compared to the equivalent directly-coded loop.

In fact, while sometimes the coders of these APIs do indeed know a few "tricks of the trade" that speed things up a bit (or have access to internal interfaces you can't use), just as often they must throw in additional logic to handle "oddball" cases that you don't care about, or simply to make the API "general".

So in most cases the choice should be based on which most reasonably fits the overall program and makes the logic clear. In some cases the explicit loop is better, especially if one can harness some of the logic (eg, to take a later-required "max" of the array values) to avoid duplication of effort.

Also, when there is a complex API function (more complex than isEqualToArray) you're not quite sure you understand, it's often better to code things in a straight-forward manner rather than deal with the complex function. Once you have the code working you can come back and "optimize" things to use the complex API.

于 2013-08-26T20:53:00.883 回答
-1

When you know both objects are Arrays, isEqualTo<Class> method is a faster way to check equality than for loop.

isEqualTo<Class> is used to provide specific checks for equality.so isEqualToArray: checks that the arrays contain an equal number of objects.

So as per my knowledge i can say isEqualToArray is better option when you know that two objects are arrays.

于 2013-08-26T12:27:18.573 回答