lately I work much with arrays and I\'m wonder.. what\'s diffrences between those two lines.
NSArray *array = [NSArray arrayWithArray:someArray];
NSMutableArray *arr = [NSMutableArray array];
for ( int i = 0; i < 10000; i ++)
{
[arr addObject:@(i*1000000ULL)];
}
// MARK
// arr = (id)[NSArray arrayWithArray:arr];
NSTimeInterval t = [NSDate timeIntervalSinceReferenceDate];
NSArray *res = nil;
for ( int i = 0; i < 10000; i ++)
{
res = [arr copy];
}
NSLog(@"time A: %f", [NSDate timeIntervalSinceReferenceDate] - t);
t = [NSDate timeIntervalSinceReferenceDate];
for ( int i = 0; i < 10000; i ++)
{
res = [NSArray arrayWithArray:arr];
}
NSLog(@"time B: %f", [NSDate timeIntervalSinceReferenceDate] - t);
time A: 1.572795, time B: 1.539150, B [NSArray arrayWithArray:] always faster but time difference very small. But if we uncomment "MARK" and get copy from NSArray instead NSMutableArray we will have other runtime A: 0.000473 time B: 1.548400 result: ~3200x times faster
Which of it is faster?
Don't worry about it. Premature optimization.
The main difference: the first approach results in an autoreleased "copy" that you don't own and don't have to release, while you do own the object created on the second line. Both arrays will be immutable, by the way.
In addition to the other answers, also note, that when someArray
is nil,
the first line will make array
point to an empty array and the second will make
it point to nil. This might be an important difference, especially in mutable arrays.
One of them is probably faster. Run them a million times and see if anyone wins.
In case of NSArray
vs NSMutableArray
, an immutable array being copied does not have to actually return a copy since it can't change. However, if you have a mutable array, it would need to be copied since you could change the original. And of course doing a mutable copy always needs to return a new object.
In your entire app, the speed and memory difference is probably not going to matter compared to everything else that's going on.
The main difference is that -copy
knows better how to copy itself (can do it more efficiently and maybe use a more adapted subclass of NSArray) while +arrayWithArray:
will create a new instance of NSArray
(well, in fact the concrete class used by Foundation for arrays) and feed it with the same list of objects from the initial object. Also it will add an extra autorelease.
So -copy
is (very very) likely more efficient.
In fact for immutable NSArrays
, -copy
is just doing -retain
, so it does not even bother creating a new instance.
The difference between the two is that the latter will be retained. The former will be autoreleased.
Both versions make a shallow copy of the array.
NSMutableArray *notMutableReally = [NSArray arrayWithArray:aMutableArray];
Should give you a compiler warning as you will be trying to assign a NSArray
to a NSMutableArray
.
Use.
NSMutableArray *mutableArrayCopy = [NSMutableArray arrayWithArray:aMutableArray];
Which is faster? Dont worry, they are all far faster than the rest of the stuff you will be doing. Check with Instruments if you really care.