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 8216573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:10:21+00:00 2026-06-07T12:10:21+00:00

Does anyone know what is more efficient with regards to speed for finding a

  • 0

Does anyone know what is more efficient with regards to speed for finding a character in an NSString or in an NSArray of the same characters?

I want to know which has the best and most efficient algorithm to find the correct value.

I effectively want to find the position of a character in the alphabet. e.g. find the position of “E” in the alphabet @"ABCDE....XYZ" or [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",...,@"X",@"Y",@"Z"];

Which is the better one to search through? NSString or NSArray?

  • 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-07T12:10:23+00:00Added an answer on June 7, 2026 at 12:10 pm

    If it’s just A-Z:

    NSString *string = @"A";
    int position = (int)[string characterAtIndex:0]-64;
    NSLog(@"%d", position);
    

    Just out of curiosity:

    NSString *alphabetString = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableArray *alphabetArray = [NSMutableArray array];
    for(int pos = 0; pos < [alphabetString length]; pos++) {
    
        [alphabetArray addObject:[alphabetString substringWithRange:NSMakeRange(pos, 1)]];
    }
    
    NSString *check = @"A";
    
    // check with rangeOfString
    NSDate *start = [NSDate date];
    for(int i = 0; i < 1000000; i++) {
    
        int position = [alphabetString rangeOfString:check].location + 1;
    }
    NSDate *end = [NSDate date];
    NSLog(@"STRING | time needed: %f", [end timeIntervalSinceDate:start]);
    
    // check with indexOfObject
    start = [NSDate date];
    for(int i = 0; i < 1000000; i++) {
    
        int position = [alphabetArray indexOfObject:check] + 1;
    }
    end = [NSDate date];
    NSLog(@"ARRAY | time needed: %f", [end timeIntervalSinceDate:start]);
    
    // check with ASCII position
    start = [NSDate date];
    for(int i = 0; i < 1000000; i++) {
    
        int position = (int)[check characterAtIndex:0]-64;
    }
    end = [NSDate date];
    NSLog(@"ASCII | time needed: %f", [end timeIntervalSinceDate:start]);
    

    Console:

    STRING | time needed: 0.156067
    ARRAY | time needed: 0.213297
    ASCII | time needed: 0.017055
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know in which programming language the Googlebot was written? Or, more generally,
Does anyone know any more details about google's web-crawler (aka GoogleBot)? I was curious
Does anyone know a better (shorter/more elegant) way than simply writing a loop and
Does anyone know how to calculate the area in common between 2 or more
Does anyone know a fast algorithm for evaluating 7 card poker hands? Something which
Does anyone know are there any issues with using more than 1 or 2
Does anyone know if there is a way to generate different code in the
Does anyone know of a free tool, similar to what is built into Visual
Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does anyone know of a mechanism in Sybase ASA 9 / Sybase SQL Anywhere

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.