目标C - 什么是枚举一个数组的最快和最有效的方法是什么? [英] Objective C — What is the fastest and most efficient way to enumerate an array?
问题描述
修改
我通过块和快速枚举和GCD之类的一些文章阅读。 @Bbum,谁写在GCD和盖帽主题的文章,称该块枚举的方法总是以最快的速度或比快速枚举等值更快。你可以阅读他的推理<一个href=\"http://stackoverflow.com/questions/4486622/when-to-use-enumerateobjectsusingblock-vs-for/4487012#4487012\">here.
尽管这是一个迷人的,知性的谈话,我同意那些谁说,这真的取决于手头的任务。
我有一些任务来完成,我需要他们快速,廉价,高效地完成。苹果给我们的的许多的作为选择如何,我们要列举一个数组,但我不知道选哪个。
快速计数
为(数组ID OBJ)
{
/ *做点什么| OBJ |。 * /
}
非并发块枚举
[阵列enumerateObjectsUsingBlock:^(ID OBJ,NSUInteger IDX,BOOL *停止){
/ *做点什么| OBJ |。 * /
}];
并发块枚举
[阵列enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(ID OBJ,NSUInteger IDX,BOOL *停止){
/ *做点什么| OBJ |。 * /
}];
GCD应用
dispatch_queue_t队列= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
dispatch_apply([数组数],队列,^(为size_t IDX){
ID OBJ = [阵列objectAtIndex:IDX];
/ *做点什么| OBJ |。 * /
});
GCD异步应用
dispatch_queue_t队列= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
dispatch_async(队列,^(无效){
dispatch_apply([数组数],队列,^(为size_t IDX){
ID OBJ = [阵列objectAtIndex:IDX];
/ *做点什么| OBJ |。 * /
});
});
或许一些与 NSBlockOperation
s或一个 NSOperationQueue
?
TIA,亚历克斯。
最快的code是code首先达到市场。
严重 - 除非你有一个可衡量的业绩问题,这个特别的选择应该占据没有更多的时间比需要回答的这些图案与适合最自然的我的项目的风格的<? / p>
请注意:从串行到并行执行通常的结果有两个问题,移动adressing性能问题;性能和放大器;并发性。
Edit
I read through some articles on blocks and fast enumeration and GCD and the like. @Bbum, who's written many articles on the subject of GCD and blocks, says that the block enumeration methods are always as fast or faster than the fast enumeration equivalents. You can read his reasoning here.
While this has been a fascinating, intellectual conversation, I agree with those who said that it really depends on the task at hand.
I have some tasks to accomplish and I need them done fast, cheap, and efficiently. Apple gives us many choices for how we want to enumerate an array, but I'm not sure which to choose.
Fast Enumeration
for (id obj in array)
{
/* Do something with |obj|. */
}
Nonconcurrent Block Enumeration
[array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
/* Do something with |obj|. */
}];
Concurrent Block Enumeration
[array enumerateObjectsWithOptions: NSEnumerationConcurrent usingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
/* Do something with |obj|. */
}];
GCD Apply
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_apply([array count], queue, ^(size_t idx) {
id obj = [array objectAtIndex: idx];
/* Do something with |obj|. */
});
GCD Async Apply
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^(void) {
dispatch_apply([array count], queue, ^(size_t idx) {
id obj = [array objectAtIndex: idx];
/* Do something with |obj|. */
});
});
Or perhaps something with NSBlockOperation
s or an NSOperationQueue
?
TIA, Alex.
The fastest code is the code that reaches the market first.
Seriously -- unless you have a measurable performance problem, this particular choice should occupy no more of your time than it takes to answer which of these patterns fits the most naturally with my project's style?
Note: adressing a performance problem by moving from serial to concurrent execution usually results having two problems; performance & concurrency.
这篇关于目标C - 什么是枚举一个数组的最快和最有效的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!