Objective C — What is the fastest and most efficient way to enumerate an array?

前端 未结 3 386
予麋鹿
予麋鹿 2020-12-23 17:24

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 th

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 17:55

    It really depends on the task at hand.

    Processing more than one iteration at a time requires spawning threads. If the logic in the iterations is parallelizable and takes more time than it would take to spawn a thread, then use threads. Also, if you have so many items in the array that it would take less to spawn a thread than to walk through the whole array, divide your array into a few pieces and process them in parallel.

    Otherwise, spawning threads to iterate through the array is overhead. Even if the OS takes care of that for you, it still does need to spawn them. That takes time and resources and context switching at runtime (depending on the number of CPUs available, load, scheduler, etc).

    It all comes down to whether spawning a new thread takes longer than walking through the whole array. You can find that out using the profiling tools.

提交回复
热议问题