See https://stackoverflow.com/a/58513549/1271826
This is quick-and-dirty example of using concurrentPerform
to parallelize a for
loop.
This will let the user select an image from their photo library and it will count how many white pixels there are. There are three implementations of this counting algorithm:
- Simple, single-threaded, nested
for
loop; - Simple, multi-threaded implementation, replacing outer
for
loop withconcurrentPerform
; and - A improved rendition of multi-threaded implementation that strides within
concurrentPerform
to try to balance amount of work done per thread (which is important in such a simple calculation).
See ImageProcessor.swift
for these three renditions.
Developed in Swift on Xcode 11.1 and Swift 5. But the basic ideas are equally applicable for different versions of Swift.
Copyright © 2019 Robert Ryan. All rights reserved.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
--
23 October 2019