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

  • Home
  • SEARCH
  • 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 542503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:27:13+00:00 2026-05-13T10:27:13+00:00

To optimize a bottleneck, I converted the creation of a large NSArray to a

  • 0

To optimize a bottleneck, I converted the creation of a large NSArray to a c-style array. (The resulting creation was 1/8 the time of the original NSArray version. Yeah!) But once it’s created, speed is no longer an issue, so I’d rather benefit from it being an NSArray again.

However, it seems ridiculously involved to convert a c-style array to an NSArray (unless I’m missing some magic initWithArrayWrapElementsInObjects method.)

As I understand the process now, I first have to create an NSMutableArray, iterate through the c-style array converting each element (floats in my case) to objects, adding each object to the NSMutableArray, then creating the NSArray with the NSMutableArray.

Is that right? There’s got to be a better way.

And help would be appreciated.

Thanks!

  • 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-05-13T10:27:13+00:00Added an answer on May 13, 2026 at 10:27 am

    There’s no direct way to take a blob of memory that you own and “convert” it cheaply into an NSArray– after all, the framework would need to then own that memory, and it doesn’t know where you got it from (malloc, stack, etc). If there were a convenience method for initWithArrayWrapElementsInObjects, it would itself need to do internally what you surmise: iterate over your provided memory and add items to itself (it’s possibly the framework could do this as quickly as a memcpy, but who knows).

    One way you could tackle this (and probably a fun learning exercise) is by actually creating your own subclass of NSArray that manages memory exactly as you want (ie, lets you create and init with whatever semantics you want), but that behaves to the outside world as an NSArray would. You can do this by inheriting from NSArray and implementing the methods count: and objectAtIndex: to operate on whatever memory you’re holding on to. Obviously, you’d need to implement the management of your own memory in the init/dealloc, etc methods as well. See this page http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html
    under “Subclassing Notes”.

    The design discussion here hinges on what your data looks like. NSArray, of course, expects its items to be Obj-C references (of type id), and not just arbitrary chunks of data. If your C-style array is holding structures or some other primitive values that aren’t object references, then this technique won’t really work for you– NSArray’s interface will never be happy with non-reference items.

    One final note: you mention taking an NSMutableArray and “creating” an NSArray with it. You should be aware that an NSMutableArray is already an NSArray, since it’s a subclass. You can use an instance of NSMutableArray anywhere you’d want an NSArray, without creating some new copy of it.

    UPDATE: Missed the note about your array containing floats. Yeah, you’re a little bit screwed here. NSArrays want objects. If the capacity doubling was the expensive part (as another poster notes), then try initWithCapacity:. If it’s the boxing/unboxing of the floats into object types, there’s nothing you can do.

    I have created (but don’t have handy) a pair of very simple classes (called like MYArray and MYMutableArray) that are intended to wrap just this kind of data with NSArray-like methods on them. But they’re not interchangeable with NSArrays. You must use them intentionally.

    UPDATE #2. I know it’s been ages since this question was live, but I just revisited it and realized there actually is a sort of clever way around this in this specific case. (You want a non-mutable NSArray from a C-style float array). You can create a custom subclass of NSArray that wraps the float values and only converts them to objects when they’re accessed via the primitives. This may have performance pitfalls in some corners (?), but it does neatly meet your requirements:

    @interface FloatProxyArray : NSArray
    {
        float * values;
        NSUInteger count;
    }
    - (id)initWithCArray:(float *)arrayOfFloats count:(int)numberOfValues;
    @end
    

    .

    @implementation FloatProxyArray
    - (id)initWithCArray:(float *)arrayOfFloats count:(int)numberOfValues
    {
        if ((self = [super init])) {
            values = (float *)malloc(numberOfValues * sizeof(float));
            if (!values) { 
                [self release]; return nil; 
            }
            memcpy(values, arrayOfFloats, numberOfValues * sizeof(float));
            count = numberOfValues;
        }
        return self;
    }
    
    - (void)dealloc
    {
        free(values);
        [super dealloc]
    }
    
    - (NSUInteger)count
    {
        return count;
    }
    
    - (id)objectAtIndex:(NSUInteger)index
    {
        if (index >= count) {
            [NSException raise:NSRangeException format:@""];
            return nil;
        }
    
        float val = values[index];
        return [NSNumber numberWithFloat:val];
    }
    @end
    

    (N.B. Written in the editor without compiling/testing.)

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

Sidebar

Related Questions

short version: Is there a good time based sampling profiler for Linux? long version:
I am trying to optimize some stored procedures on a SQL Server 2000 database
We need to optimize the text rendering for a C# Windows Forms application displaying
Does the compiler optimize out any multiplications by 1? That is, consider: int a
Does the C++ compiler optimize the multiply by two operation x*2 to a bitshift
I need to optimize code to get room for some new code. I do
I'm trying to optimize query performance and have had to resort to using optimizer
I am looking to optimize a process that runs continually and makes frequent calls
Or: Should I optimize my string-operations in PHP? I tried to ask PHP's manual
I am trying to optimize a small, highly used function which uses the high

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.