简体   繁体   中英

Object Array in Objective C with ARC

I am writing an application that I'd like to speed up. One way I have thought to do this is by switching from using NSArray and NSMutableArray to using straight c-style arrays of pointers.

I had tried naively to just do:

MyObject** objects = (MyObject**) malloc(N/2*sizeof(MyObject*))

This reports a compiler error when using ARC as it doesn't know what to do with a ** object; this can be fixed by adding a bridge directive.

My question is how is this memory being handled and how to do memory management mixing C and Objective-C objects.

Two solutions are

MyObject* __weak* objects = (MyObject* __weak*) malloc(N/2*sizeof(MyObject*));

MyObject* __strong* objects = (MyObject* __strong*) malloc(N/2*sizeof(MyObject*));

What are the differences between those two arrays and how do I go about freeing/releasing them when done. Are NSArrays optimized to the point where this wouldn't result it much of a speed up?

Are NSArrays optimized to the point where this wouldn't result it much of a speed up?

Yes.

You should profile your code in Instruments -- chances are that even if you make heavy use of arrays, you're going to find that your code spends most of its time in places other than NSArray methods like -objectAtIndex: .

Taking that a step further, you should really be able to tell us whether NSArray is optimized sufficiently that you don't need to improve it. If you're looking to speed up your code by replacing NSArray, you should have already profiled your code and identified the expensive parts. Don't just guess at what needs to be improved; measure it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM