NSMutableArray *mutableItems = [[NSMutableArray alloc] initWithCapacity:1000];
for (int i = 0; i < 10000; i++) {
[mutableItems addObject:[NSDate date]];
}
[mutableItems release];
NSArray *items = [[[NSArray alloc] init] autorelease];
or (int i = 0; i < 10000; i++) {
items = [items arrayByAddingObject:[NSDate date]];
}
Method 1: 0.011135 seconds.
Method 2: 9.712520 seconds.
NSMutableArray *items = [[NSArray alloc] init];
for (int i = 0; i < 10000; i++) {
items = [items addObject:[NSDate date]];
}
NSArray *mutableItems = [array arrayWithArray:items];
[items release];
Your code is wrong. [NSArray alloc] returns an immutable array. And the red-hilited code is nonsense.Your Method2 is not good enough, here is mine, can you test time for me? I am not sure how to test it🙁
Code:NSMutableArray *items = [[NSArray alloc] init]; for (int i = 0; i < 10000; i++) { [COLOR="Red"]items = [/COLOR][items addObject:[NSDate date]]; } NSArray *mutableItems = [[COLOR="Red"]array[/COLOR] arrayWithArray:items]; [items release];
Your Method2 is not good enough, here is mine, can you test time for me? I am not sure how to test it🙁
[...]
It is said the mutable array is not better performance than immutable array, so that I want to use a immutable array, but immutable array cannot be added with items, so I use a temp mutable array to add items, then copy it to immutable array and use it for long time without modify.Your code is wrong. [NSArray alloc] returns an immutable array. And the red-hilited code is nonsense.
Explain exactly what problem you're trying to solve, that you need to copy an NSMutableArray.
Also explain why the -copy method won't solve this problem. If you're not familiar with -copy, you should look it up in the reference docs for NSArray, NSMutableArray, and the NSCopying protocol. You're the only one who can do this, because no one else knows what problem you're trying to solve.
It is said the mutable array is not better performance than immutable array, so that I want to use a immutable array, but immutable array cannot be added with items, so I use a temp mutable array to add items, then copy it to immutable array and use it for long time without modify.
Can you show me your good idea code if you have?
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
// fill the array here
NSArray *immutableArray = [[NSArray alloc] initWithArray:mutableArray];
[mutableArray release];
This is what I want to know, is this method better performance than your method1 when use it it, not only fill it, because last code in above code take a few time than your method1, but if it has a very good performance than your method1 when refer it a lot of times, I this above code is better, may I make sense?If you fear that NSMutableArray for some reason causes overhead down the road when you no longer modify its contents, you can always switch to an immutable array:
Code:NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; // fill the array here NSArray *immutableArray = [[NSArray alloc] initWithArray:mutableArray]; [mutableArray release];
This is what I want to know, is this method better performance than your method1 when use it it, not only fill it, because last code in above code take a few time than your method1, but if it has a very good performance than your method1 when refer it a lot of times, I this above code is better, may I make sense?
Well, I will follow it.Well, you're only going to create the immutable array from the mutable one once as long as you're not using HUGE arrays, it shouldn't take long compared to filling up the array in first place.
But only testing in your particular situation can reveal what's best. Just implement your various ideas, test them and see what turns out to be the best.
You are right, like you said, I am afraid of about performance, I am not sure why, maybe is due to my Personality, though I see it is better to optimize after I created my app and it has very bad performance🙁On a side note: You seem to be very concerned about optimization. Make sure you read this Wikipedia article, and remember that "premature optimization is the root of all evil".
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
// process here
NSTimeInterval end = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"elapsed time = %g", (end - start) * 1000.0);
Well, you're only going to create the immutable array from the mutable one once as long as you're not using HUGE arrays, it shouldn't take long compared to filling up the array in first place.
Well, I will follow it.
You are right, like you said, I am afraid of about performance, I am not sure why, maybe is due to my Personality, though I see it is better to optimize after I created my app and it has very bad performance🙁