There should be an easier way to deal with arrays!

To optimize the bottleneck, I converted the creation of a large NSArray into a c-style array. (As a result, the creation was 1/8 time of the original version of NSArray. Yes!) But once it was created, speed is no longer a problem, so I would prefer it to become NSArray again.

However, it seems ridiculous to participate in converting the c-style array to NSArray (unless I miss some magic initWithArrayWrapElementsInObjects method.)

As I understand the process now, I first need to create an NSMutableArray, iterate through a c-style array that converts each element (floats in my case) into objects, add each object to an NSMutableArray, and then create an NSArray using NSMutableArray.

It is right? There has to be a better way.

And help will be appreciated.

Thanks!

+5
source share
4 answers

There is no direct way to take your own memory block and “convert” it cheaply into NSArray. In the end, the structure will then have to own this memory, and it does not know where you got it from (malloc, stack, etc.). If there was a convenience method for initWithArrayWrapElementsInObjects, he himself would have to do what you assume internally: iterate over its provided memory and add elements to itself (perhaps the infrastructure can do this as fast as memcpy, but who knows).

(, , ) NSArray, , (.. ), , NSArray. , NSArray count: objectAtIndex: , . , init/dealloc .. . http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html " ".

, . NSArray, , , Obj-C ( id), . C- , , . NSArray .

: NSMutableArray NSArray. , NSMutableArray NSArray, . NSMutableArray , NSArray, - .

UPDATE. , . , . NSArrays . ( ), initWithCapacity:. / , .

( ) ( MYArray MYMutableArray), NSArray-. NSArrays. .

# 2. , , , , . ( , NSArray float C-style). NSArray, float , . (?), :

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

+7

, NSMutableArray, - initWithCapacity, , .

, NSArrays NSMutableArrays , .

+3

NSArray ?

, C, NSArray, . ... initWithObjects, NSNumber, .

NSArray, -, , NSArray, , , NSArray ( , ).

+1
source

The “best” optimization (for speed) would almost certainly be to avoid use NSArrayat all, if possible.

0
source

All Articles