Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:15+00:00 2026-06-13T12:55:15+00:00

Is there any quick and efficient way of finding max int value in NSArray

  • 0

Is there any quick and efficient way of finding max int value in NSArray that consists of NSDictionary objects? I mean sure, I can implement for cycle going through, but I am looking for some API function that’s already tweaked to maximum speed since it’s operating with fairly large amount of data.

[(int, string, string), (int, string, string), (int, string, string)]

I tried working with valueForKeyPath but that really didn’t help me so far, since it works with “ordinary” NSArray objects.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:55:16+00:00Added an answer on June 13, 2026 at 12:55 pm

    Out of curiosity, I did a little comparison of valueForKeyPath: vs simple iteration. On my iMac core i7 running OS X 10.8.2, the simple iteration is about twice as fast for an array of 10M elements.

    Here’s the test program I made:

    #import <Foundation/Foundation.h>
    #undef NDEBUG
    #import <assert.h>
    #import <limits.h>
    #import <stdio.h>
    #import <stdlib.h>
    
    #define ELEMENTS_IN_ARRAY 10000000
    
    NSArray *newArrayWithDictionaryElementCount(int count) {
        NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:count];
        for (int i = 0; i < count; ++i) {
            [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                            [NSString stringWithFormat:@"value%d", i], @"string",
                            [NSNumber numberWithInt:rand()], @"int",
                            nil]];
        }
        return arr;
    }
    
    int maxIntValueByKeyPathInArray(NSArray *arr) {
        return [(NSNumber *)[arr valueForKeyPath:@"@max.int"] intValue];
    }
    
    int maxIntValueByIterationInArray(NSArray *arr) {
        int max = INT_MIN;
        for (NSDictionary *dict in arr) {
            int val = [(NSNumber *)[dict valueForKey:@"int"] intValue];
            if (val > max) {
                max = val;
            }
        }
        return max;
    }
    
    NSTimeInterval timeExecutionOf(void(^blockToTime)(void)) {
        NSDate *start = [NSDate date];
        blockToTime();
        return -[start timeIntervalSinceNow];
    }
    
    int main (int argc, const char *argv[]) {
        srand(time(NULL));
        @autoreleasepool {
            NSArray *arr = newArrayWithDictionaryElementCount(ELEMENTS_IN_ARRAY);
            assert(maxIntValueByIterationInArray(arr) == maxIntValueByKeyPathInArray(arr));
            (void) printf("Time by key path: %f s\n", timeExecutionOf(^{ maxIntValueByKeyPathInArray(arr); }));
            (void) printf("Time by iteration: %f s\n", timeExecutionOf(^{ maxIntValueByIterationInArray(arr); }));
        }
        return 0;
    }
    

    The results on my machine:

    $ clang -fobjc-arc -framework Foundation -O4 -march=corei7 -o arraytest arraytest.m
    $ ./arraytest
    Time by key path: 1.809646 s
    Time by iteration: 0.886023 s
    

    My hypothesis is that the iterative solution is already about as fast as can be for these data structures; there is no getting around having to do the dictionary look-up for each array element. Furthermore, this custom-made iterative solution benefits from knowing that all the NSNumber objects have int values; using isGreaterThan: for comparison slows things down somewhat (but it’s still faster than valueForKeyPath:). Any general-purpose library method would almost certainly incur that penalty internally…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any way of quick jump to the last modified line? eclipse has
A quick question, for a quick answer (since I'm not finding any): Is there
Is there any way I can set a formatter on models that will convert
Is there any quick and easy way to use a sprite to create a
Is there any quick way I can get the number of strings within a
Just wondering, is there any quick way to count all the NULL values (from
Is there any quick way to get a (random) permutation of a given hash?
Is there any quick way to know if an image is referenced or used
Is there any quick way to copy eclipse and android development environment from one
Is there any quick guide/reference to list all the countries and its states/provinces? Like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.