Iteration

Even something as simple as a for-loop iterating over data performing work can be enhanced by GCD. The classic way of iterating through an array and performing an operation on each element is through a for loop.

e​n​u​m​ ​{​
 ​ ​ ​ ​k​N​u​m​b​e​r​C​o​u​n​t​ ​=​ ​2​0​0​0​0​0​
}​;​

s​t​a​t​i​c​ ​i​n​t​ ​n​u​m​b​e​r​s​[​k​N​u​m​b​e​r​C​o​u​n​t​]​;​
s​t​a​t​i​c​ ​i​n​t​ ​r​e​s​u​l​t​s​[​k​N​u​m​b​e​r​C​o​u​n​t​]​;​

f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​k​N​u​m​b​e​r​C​o​u​n​t​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​r​e​s​u​l​t​s​[​i​]​ ​=​ ​W​o​r​k​ ​(​n​u​m​b​e​r​s​,​ ​i​)​;​
}​

You could walk through the array doing work that is not really interrelated and then sift through the work to make the final conclusion, like a map/reduce operation.

You can ...

Get Advanced Mac OS X Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.