Could anyone help me with this problem?
I'm trying to achieve more efficiency that's why I tried to parallel my calculations. After a few tests the results showed me that nothing is faster than calculation on 1 thread. its only 25% of processor load in both cases (1 thread and 4 threads). Could any one know why it's happening? Is there anything I can do to achieve 100% of efficiency (even 90% be better than 25%)?
Below you can see an example code:
ToolsThread = class(TThread)
public
procedure Execute(); override;
procedure QuickSortT(var dict: TArray<AnsiString>; iLo, iHi: Integer);
Procedure QSortT(var dict: TArray<AnsiString>);
constructor Create();
var
tab : TArray<AnsiString>;
tmp1: Longint;
end;
procedure ToolsThread.QuickSortT(var dict: TArray<AnsiString>; iLo, iHi: Integer);
var
Lo, Hi: Longint;
Pivot: Pointer;
T: Pointer;
begin
Lo := iLo;
Hi := iHi;
Pivot := pointer(dict[(Lo + Hi) shr 1]); // shr 1 is slightly faster than div 2;
repeat
while dict[Lo] < AnsiString(Pivot) do Inc(Lo);
while dict[Hi] > AnsiString(Pivot) do Dec(Hi);
if Lo <= Hi then
begin
T := pointer(dict[Lo]);
pointer(dict[Lo]) := pointer(dict[Hi]);
pointer(dict[Hi]) := T;
Inc(Lo) ;
Dec(Hi) ;
end;
until Lo > Hi;
if Hi > iLo then QuickSort(dict, iLo, Hi) ;
if Lo < iHi then QuickSort(dict, Lo, iHi) ;
end;
Procedure ToolsThread.QSortT(var dict: TArray<AnsiString>);
begin
QuickSort(dict, 0, Length(dict)-1);
end;
procedure ToolsThread.Execute();
var
tmp1, tmp2 : Longint;
dict: TArray<AnsiString>;
begin
SetLength(dict, 10000000);
for tmp1:= 0 to 10000000-1 do
dict[tmp1] := IntToStr(Random(high(integer)));
QSortT(dict);
end;
Procedure Main;
var
Th1, Th2, Th3, Th4: ToolsThread;
begin
Th1 := ToolsThread.Create();
Th2 := ToolsThread.Create();
Th3 := ToolsThread.Create();
Th4 := ToolsThread.Create();
debug('Start THR');
Th1.Start;
Th2.Start;
Th3.Start;
Th4.Start;
th1.WaitFor;
th2.WaitFor;
th3.WaitFor;
th4.WaitFor;
debug('THR Done');
end;
Corrected according suggestions. Still 25% CPU load (5-8% per thread)
SOLVED! There is a general problem with some Delphi memory management in multiprocessing. It's not fastMM4 problem and it is resolvable only as workaround for now.