我有一个方法来构建和返回一个数组:
- (NSArray *)foo
{
NSUInteger capacity = [self getArrayCapacity];
if (capacity == 0) {
return @[];
} else {
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
// add elements to the array, as many as capacity
...
return array;
}
}
如果我将代码简化如下,使用的内存或性能是否存在差异:
- (NSArray *)fooSimplified
{
NSUInteger capacity = [self getCapacity];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:capacity];
// add elements to the array, as many as capacity
...
return array;
}
}
所以当它不返回时capacity == 0
,@[]
它会返回[[NSMutableArray alloc] initWithCapacity:0]
是否存在性能或内存损失/差异?