4

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).

6
  • NSArray isn't multidimensional.. so you have 1 array nesting the other?
    – Daij-Djan
    Aug 26, 2013 at 12:30
  • or an array full of dictionaries? or one array being 11x160 with you doing the math yourself?
    – Daij-Djan
    Aug 26, 2013 at 12:32
  • Sorry, each element in the array is a dictionary with 11 entries, edited it.
    – Lord Zsolt
    Aug 26, 2013 at 12:33
  • 2
    Three observations: First, if you want to optimize your first example, you could use fast enumeration (at least on one of the arrays). I'd wager that the built-in method uses fast enumeration. Second, it seems like these two snippets do not do the same thing. One only considers the Update key, the other looks at the whole entry. Third, if you're concerned about performance, you should benchmark it and measure the actual performance.
    – Rob
    Aug 26, 2013 at 12:40
  • Yes, thats the whole debate about. My loop with only considering one element vs Apple's, which considers everything.
    – Lord Zsolt
    Aug 26, 2013 at 12:44

3 Answers 3

4

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.

2
  • Then again I assume comparing 1600 values would only save me nanoseconds.
    – Lord Zsolt
    Aug 26, 2013 at 12:02
  • 2
    That's the reason why I would favor the code that is easier to read, especially when you write files to disk, which is slow anyway.
    – eik
    Aug 26, 2013 at 19:00
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.

2
  • I disagree with your last remark. One should not fear complex API, but strive to understand them. I also disagree about implementing something in an unoptimal way - how many times have we heard "but it works, why change it"; that horrible sentence that haunts us all. Usually there is no time to come back to it.
    – Léo Natan
    Aug 26, 2013 at 21:08
  • @LeoNatan - Starting with you second comment first, have you ever heard of "premature optimization"? With regard to the first, especially with newbies it's better that they go ahead and code the loop and become comfortable with doing that, so they can better "think in loops". Plus, even for an experienced programmer (40 years here) it's often not worth the effort to understand a complex API when one can do the needed operation with a simple loop or some such, especially since, as I said, very often the complex API is slower (and generally harder for a maintainer to understand).
    – Hot Licks
    Aug 26, 2013 at 22:17
-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.

2
  • 1
    Yes, thats what I thought as well, assuming it first checks for size, if those differ, then it doesn't iterate thought the whole array.
    – Lord Zsolt
    Aug 26, 2013 at 12:34
  • While this surely works, it should check all of the dictionaries' items - which was just to avoid, as stated in the question.
    – Eiko
    Aug 26, 2013 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.