Map
Let us move from a very complicated pattern to something really simple. The Map pattern was designed for one simple task. It takes an array, runs a mapping function on all of its elements in parallel, and returns a new array containing modified data. It implements the same functionality as Spring’s TEnumerable.Select
, except that it uses multiple worker threads. Besides mapping, it also supports data filtering, so we can also use it as a fast filter or a combined mapper/filter.
The ParallelMap
demo implements two approaches to solve the same problem. They both create an array of 1,000,000 large-ish integer numbers (from 1,000,000 up), check each element in that array for primality, and generate a new array containing only prime numbers. They both measure the time used to check primality and generate the new array but ignore the time needed to prepare the input data.
The Serial button triggers an event handler, shown next, which does this in an idiomatic Delphi way...