Skip to Main Content
Delphi High Performance
book

Delphi High Performance

by Primož Gabrijelčič
February 2018
Intermediate to advanced content levelIntermediate to advanced
336 pages
7h 52m
English
Packt Publishing
Content preview from Delphi High Performance

Parallelizing a loop

Let us now return to Mr. Smith's problem. He is calling the FindPrimes method, which checks the primality of natural numbers in a for loop. I've shown that code at the beginning of the chapter, but for convenience I'll reprint it here:

function FindPrimes(lowBound, highBound: integer): integer;var  i: Integer;begin  Result := 0;  for i := lowBound to highBound do    if IsPrime(i) then      Inc(Result);end;

When he calls, for example, FindPrimes(1,10000000), the computer spends a long time doing the calculation during which time only one CPU is busy. That is not surprising given that in any Delphi program only one thread is running by default. If we want to make other CPUs busy too, we have to run multiple threads.

In his case, it ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Delphi Cookbook

Delphi Cookbook

Daniele Teti

Publisher Resources

ISBN: 9781788625456Supplemental Content