GCD Poor Performance

前端 未结 2 715
忘掉有多难
忘掉有多难 2021-01-30 10:12

As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because

2条回答
  •  清酒与你
    2021-01-30 10:19

    In your code you are delaying the work you need to do for each object until the end of the nested for loop. That said, when the loop finishes you will have one operation with lots of blocks for a bunch of objects and you won't thereby take advantage of the GCD properly.

    I would suggest you create one NSBlockOperation for each object and add it to the NSOperationQueue in the end of each for (int j = i + 1; j < count; j++) iteration.

    This way, the system will begin processing the work you need to do for each object as soon as the iteration ends.

    Also keep in mind that the queue shouldn't be much larger than the available processors, otherwise you will have some overhead on the thread switch process that will compromise speed.

提交回复
热议问题