Reputation: 6557
I would like to know what isEqualToArray
actually does...
I have an array with size 160, each containing a dictionary with 11 entries, but I can do the comparison simply based on the first column (contains the date when the row was changed).
Now I can do that with a simple 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");
Or I can use the built-in isEqualToArray method:
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];
}
Now, if assuming isEqualToArray
just invokes isEqualTo
for each cell, the for-loop method runs for 1/11 of the time isEqualToArray
does (only need to compare one column instead of 11).
Maybe I'm just too much into optimizing... (I've been at many contests where runtime was limited and I'm feeling the after-effects).
Upvotes: 4
Views: 868
Reputation: 47729
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.
Upvotes: 2
Reputation: 4610
The Documentation says:
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.
Upvotes: 4
Reputation: 8790
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.
Upvotes: -1