Which one is faster? for-loop or isEqualToArray

I would like to know what isEqualToArray actually does ...

I have an array of size 160, each of which contains a dictionary with 11 entries, but I can make a comparison simply based on the first column (contains the date the row changed).

Now I can do it with a simple looping loop:

  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 isEqualToArray built-in 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 it is assumed that isEqualToArray simply calls isEqualTo for each cell, the for-loop method works in 1/11 of the time isEqualToArray (you only need to compare one column instead of 11).

Perhaps I’m just optimizing too much ... (I have been to many contests where the execution time is limited and I feel the consequences).

+4
source share
3 answers

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

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

So, according to my knowledge, I can say that isEqualToArray is the best option when you know that two objects are arrays.

-one
source

The documentation reads:

Two arrays have the same content if each of them contains the same number of objects and objects at a given index in each array that satisfies the isEqual: test.

So basically you are right.

From a design point of view, I would either go to isEqualToArray: as it simplifies the code understanding or introduces BOOL hasUpdates if you are worried about efficiency, which has the additional advantage that you do not have to save two copies in memory.

+4
source

I suspect that many people mistakenly believe that performance is proportional to the number of initial statements executed, and that the isEqualToArray function is dazzling quickly compared to equivalent direct code.

In fact, although sometimes the encoders of these APIs do know a few "tricks in the trade" that speed things up a bit (or have access to internal interfaces that you cannot use), they also often need to be thrown in additional logic to handle "extra" cases that you don’t need, or just make the API "general."

Therefore, in most cases, the choice should be based on what most reasonably corresponds to the general program and makes the logic understandable. In some cases, an explicit loop is better, especially if you can use some logic (for example, to accept a later "maximum" array value) to avoid duplication of effort.

Also, when there is a complex API function (more complex than isEqualToArray), you are not entirely sure that you understand that it is often better to code things straightforwardly rather than dealing with a complex function. After you work with the code, you can go back and β€œoptimize” things to use a complex API.

+2
source

Source: https://habr.com/ru/post/1498947/


All Articles