[翻译]  Creating an NSMutableArray with a literal via mutableCopy or arrayWithArray: [duplicate]

[CHINESE]  通过mutableCopy或arrayWithArray创建带有文字的NSMutableArray:[复制]


Possible Duplicate:
Is literal creation of an NSMutableDictionary less efficient than the class helper method?

可能重复:NSMutableDictionary的文字创建效率低于类助手方法吗?

According to the WWDC video that introduces ObjectiveC literals, NSMutableArrays can be initialized like so:

根据引入ObjectiveC文字的WWDC视频,NSMutableArrays可以像这样初始化:

[NSMutableArray arrayWithArray:@[]];

but what if we were to do it like this:

但如果我们这样做会怎么样:

[@[] mutableCopy];

I know that one is initializing the array, and the other is just providing a shallow copy; thus the memory management is different. But if we are using ARC, what are the disadvantages of using the latter? Is it more expensive? ARC probably handles both differently, but I'm just wondering if using mutableCopy is 'worse' or not.

我知道一个是初始化数组,另一个是提供浅拷贝;因此内存管理是不同的。但是,如果我们使用ARC,使用后者有什么缺点?它更贵吗? ARC可能会以不同的方式处理,但我只是想知道使用mutableCopy是否“更糟”。

1 个解决方案

#1


6  

In the former case, you end up with two objects added to the current autorelease pool: the NSArray literal and the NSMutableArray that you’re creating from it. In the latter, the literal NSArray gets added to the autorelease pool but the NSMutableArray copied from it does not. So the first case is slightly worse, performance-wise, in that draining the autorelease pool (e.g. at the end of the run-loop cycle) requires releasing both objects in succession, while in the second case the copy will get released separately from the autoreleased literal. In practice, the time difference will be infinitesimal.

在前一种情况下,最终会将两个对象添加到当前自动释放池中:NSArray文字和您从中创建的NSMutableArray。在后者中,文字NSArray被添加到自动释放池中,但是从它复制的NSMutableArray没有。所以第一种情况稍微差一点,性能方面,因为耗尽自动释放池(例如在运行循环周期结束时)需要连续释放两个对象,而在第二种情况下,副本将与自动释放的文字。在实践中,时差将是无穷小的。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号