简体   繁体   English

NSArray计数方法有多贵?

[英]How expensive is the NSArray count method?

I am in a situation that I need to get the items in an array and time is sensitive. 我处于一种情况,我需要获取数组中的项目,时间是敏感的。 I have the option of using a separate variable to hold the current count or just use NSMutableArray's count method. 我可以选择使用单独的变量来保存当前计数,或者只使用NSMutableArray的count方法。

ex: if (myArray.count == ... ) or if (myArrayCount == ...) 例如: if (myArray.count == ... )if (myArrayCount == ...)

How expensive is it to get the counting of items from the count method of an array? 从数组的count方法计算项目的数量是多少?

正确的答案是,速度没有区别,所以你希望我的孩子访问数组的计数:)

Fetching NSArray 's count method is no more expensive then fetching a local variable in which you've stored this value. 获取NSArraycount方法并不比获取存储此值的局部变量更昂贵。 It's not calculated when it's called. 它被调用时不计算。 It's calculated when the array is created and stored. 它是在创建和存储数组时计算的。

For NSMutableArray , the only difference is that the property is recalculated any time you modify the contents of the array. 对于NSMutableArray ,唯一的区别是NSMutableArray修改数组内容时都会重新计算属性。 The end result is still the same--when you call count , the number returned was precalculated. 最终结果仍然相同 - 当您调用count ,返回的数字是预先计算的。 It's just returning the precalculated number it already stored. 它只是返回已存储的预先计算的数字。

Storing count in a variable, particularly for an NSMutableArray is actually a worse option because the size of the array could change, and access the count in this variable is not faster whatsoever. 将计数存储在变量中,特别是对于NSMutableArray实际上是一个更糟糕的选择,因为数组的大小可能会改变,并且访问此变量中的计数并不是更快。 It only provides the added risk of potential inaccuracy. 它只会增加潜在不准确的风险。

The best way to prove to yourself that this is a preset value that is not calculated upon the count method being called is to create two arrays. 向自己证明这是在调用count方法时不计算的预设值的最佳方法是创建两个数组。 One array has only a few elements. 一个数组只有几个元素。 The other array has tens of thousands of elements. 另一个阵列有数万个元素。 Now time how long it takes count to return. 现在count返回需要多长时间。 You'll find the time for count to return is identical no matter the size of the array. 无论数组的大小如何,您都会发现返回count的时间是相同的。

As a correction to everyone above, NSArray does not have a count property. 作为对上述每个人的修正,NSArray没有计数属性。 It has a count method. 它有一个计数方法。 The method itself either physically counts all of the elements within the array or is a getter for a private variable the array stores. 该方法本身要么物理地计算数组中的所有元素,要么是数组存储的私有变量的getter。 Unless you plan on subclassing NSArray and create a higher efficient system for counting dynamic and/or static arrays... you're not going to get better performance than using the count method on an NSArray. 除非你计划继承NSArray并创建一个更高效的系统来计算动态和/或静态数组......你不会比在NSArray上使用count方法获得更好的性能。 As a matter of fact, you should count on the fact that Apple has already optimized this method to it's max. 事实上,你应该依靠Apple已经优化了这种方法的事实。 My main ponder after this is that if you are doing an asynchronous call and your focus is optimizing the count of an NSArray how do you not know that you are seriously doing something wrong. 我的主要思考是,如果你正在进行异步调用并且你的重点是优化NSArray的数量,你怎么不知道你是在认真做错了什么。 If you are performing some high performance hitting method on the main thread or such... you should consider optimizing that. 如果你在主线程上执行一些高性能的击球方法......你应该考虑优化它。 The performance hit of iterating and counting through the array using NSArray's count method should in no way effect your performance to any noticeable rate. 使用NSArray的计数方法迭代和计算数组的性能不会影响您的性能到任何明显的速率。

You should read up more on performance for NSArrays and NSMutableArrays if this is truly a concern for you. 如果这对您来说真的是一个问题,您应该阅读更多有关NSArrays和NSMutableArrays的性能的信息。 You can start here: link 你可以从这里开始: 链接

If you need to get the item**s** then getting the count is not time critical. 如果您需要获得项目** s **,那么获得计数并不是时间关键。 You'd also want to look at fast enumeration, or using enumeration with dispatch blocks, especially with parallel execution. 您还需要查看快速枚举,或使用带调度块的枚举,尤其是并行执行。

Edit: 编辑:

Asa's is the most correct answer. Asa是最正确的答案。 I misunderstood the question. 我误解了这个问题。

Asa is right because the compiler will automatically optimize this and use the fastest way on its own. Asa是对的,因为编译器会自动优化它并自己使用最快的方法。


TheGamingArt is correct about NSArray being as optimal as could be. 关于NSArray尽可能优化的游戏艺术是正确的。 However, this is only for obj-c. 但是,这仅适用于obj-c。

Don't forget you have access to c and c++ which means you can use vectors which should be only 'slightly' faster considering it won't use obj-c messaging. 不要忘记你可以访问c和c ++,这意味着你可以使用的矢量应该只是“稍微”一点,因为它不会使用obj-c消息传递。 However, it wouldn't surprise me if the difference isn't noticeable. 但是,如果差异不明显,我也不会感到惊讶。 c++ vector benchmarks: http://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html c ++矢量基准测试: http//baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html

This is a good example of Premature Optimization ( http://c2.com/cgi/wiki?PrematureOptimization ). 这是早熟优化的一个很好的例子( http://c2.com/cgi/wiki?PrematureOptimization )。 I suggest you look into GCD or NSOperations ( http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues ) 我建议你研究一下GCD或NSOperations( http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM