在Objective-C中使用c风格的数组

时间:2021-09-14 19:12:28

Let's say that I want to use C-style arrays of NSObjects instead of using NSArray. Does this have any performance penalties over the usage of NSArray?

假设我想使用c样式的NSObjects数组,而不是使用NSArray。这对NSArray的使用有任何性能损失吗?

2 个解决方案

#1


2  

Although there is no performance penalty for it, there is certainly a loss of flexibility: unlike NSMutableArrays, your C arrays are fixed-size. You would not be able to use fast enumeration with C arrays, too, having to resort to using array indexes.

虽然它没有性能损失,但是肯定会失去灵活性:与nsmutablearray不同,您的C数组是固定大小的。您也不能使用C数组快速枚举,必须使用数组索引。

If these limitations are OK with your requirements, C arrays should work. They play nicely with ARC, too: once a C array of strong references goes out of scope, ARC releases all instances that are not set to nil.

如果这些限制符合您的要求,那么C阵列应该可以工作。它们也可以很好地使用ARC:一旦C数组的强引用超出范围,ARC将释放所有未设置为nil的实例。

#2


3  

There are no performance penalties, indeed technically there should be a performance improvement. What you trade away is quite a lot of the NSArray functionality and a reasonable amount of encapsulation, giving you some syntax headaches and a risk of memory leakage if you're not careful.

没有性能罚款,实际上技术上应该有性能改进。您所交换的是相当多的NSArray功能和合理的封装,如果您不小心的话,这会给您带来一些语法麻烦和内存泄漏的风险。

That said, one app I worked on involved a 2d array of data. Conveniently the array was a fixed size, known in advance. I hid the logic for that inside a custom analogue of NSArray that took two-dimensional indices. An early implementation used a dictionary with NSIndexPaths as keys. That was quite slow. I tried an NSArray of NSArrays. That was slower. I tried a 2d C array and that was significantly faster. Having taken the time to balance my retains and releases there were no ill consequences for performance.

也就是说,我开发的一个应用程序涉及一个二维数据数组。阵列是固定的尺寸,预先知道。我把它的逻辑隐藏在一个自定义的NSArray类比中,它取二维指标。一个早期的实现使用带有NSIndexPaths作为键的字典。这是非常缓慢。我试过NSArray的疗法。这是慢的。我尝试了一个2d C数组,这明显更快。在花时间平衡我的保留和释放之后,对性能没有不良后果。

#1


2  

Although there is no performance penalty for it, there is certainly a loss of flexibility: unlike NSMutableArrays, your C arrays are fixed-size. You would not be able to use fast enumeration with C arrays, too, having to resort to using array indexes.

虽然它没有性能损失,但是肯定会失去灵活性:与nsmutablearray不同,您的C数组是固定大小的。您也不能使用C数组快速枚举,必须使用数组索引。

If these limitations are OK with your requirements, C arrays should work. They play nicely with ARC, too: once a C array of strong references goes out of scope, ARC releases all instances that are not set to nil.

如果这些限制符合您的要求,那么C阵列应该可以工作。它们也可以很好地使用ARC:一旦C数组的强引用超出范围,ARC将释放所有未设置为nil的实例。

#2


3  

There are no performance penalties, indeed technically there should be a performance improvement. What you trade away is quite a lot of the NSArray functionality and a reasonable amount of encapsulation, giving you some syntax headaches and a risk of memory leakage if you're not careful.

没有性能罚款,实际上技术上应该有性能改进。您所交换的是相当多的NSArray功能和合理的封装,如果您不小心的话,这会给您带来一些语法麻烦和内存泄漏的风险。

That said, one app I worked on involved a 2d array of data. Conveniently the array was a fixed size, known in advance. I hid the logic for that inside a custom analogue of NSArray that took two-dimensional indices. An early implementation used a dictionary with NSIndexPaths as keys. That was quite slow. I tried an NSArray of NSArrays. That was slower. I tried a 2d C array and that was significantly faster. Having taken the time to balance my retains and releases there were no ill consequences for performance.

也就是说,我开发的一个应用程序涉及一个二维数据数组。阵列是固定的尺寸,预先知道。我把它的逻辑隐藏在一个自定义的NSArray类比中,它取二维指标。一个早期的实现使用带有NSIndexPaths作为键的字典。这是非常缓慢。我试过NSArray的疗法。这是慢的。我尝试了一个2d C数组,这明显更快。在花时间平衡我的保留和释放之后,对性能没有不良后果。