GCD vs custom queue

不羁岁月 提交于 2020-01-15 05:54:26

问题


I was wondering what is the difference in performance between these two.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    // perform complex operation

    // dispatch back to main thread to update UI

});



dispatch_async(_myCustomConcurrentQueue, ^{

    // perform complex operation

    // dispatch back to main thread to update UI

});

My assumption is the GCD is used across the os and other applications, and it will need to perform very quick background tasks, and be finished quick. And custom queues that are created are separate from GCD and they can run a different task, and will be added back to the pool once they are released. And so my assumption is that my customQueue performs better than GCD for a complex operation.

What are your thoughts? Which performs better? Are they the same?


回答1:


While the high-priority global queue might theoretically be faster (since you don't have to create the queue, slightly different thread priority), the difference between that and your own custom concurrent queue is unlikely to be observable. There are two reasons, though, that you might want to use your own custom queues:

  1. Certain features, notably dispatch barriers, are unavailable in global queues, so if you need those features, you'll want to use custom queue.

  2. Debugging your app, it can also be useful to use your own queues with meaningful names, so that you can more easily identify the individual threads in the debugger.

But there are no material performance reasons to choose high priority global concurrent queue vs a custom concurrent queue.




回答2:


Well, you don't say how _myCustomConcurrentQueue was created (it could be a serial queue or a concurrent queue), but assuming that it's a concurrent queue then it will have potentially a different priority than the global concurrent queues, both in terms of how GCD dispatches blocks or functions from its internal "queue of queues" blocklist and in the actual thread priority of the thread(s) created to do the work!

Please read the dispatch_queue_create(3) man page and pay specific attention to the "GLOBAL CONCURRENT QUEUES" section. It contains some very informative verbiage on this exact topic (too much to cut-and-paste here).




回答3:


I'm pretty sure that if you create your own queue, it ultimately gets added to the GCD behind the scenes. I guess it's more of a preference thing.



来源:https://stackoverflow.com/questions/22029166/gcd-vs-custom-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!