2

Which one is faster? For-loop or isEqualToArray

 3 years ago
source link: https://www.codesd.com/item/which-one-is-faster-for-loop-or-isequaltoarray.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Which one is faster? For-loop or isEqualToArray

advertisements

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


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK