Hello
I've decided to post here some interesting results in the hope that it might help others.
As I need threading for my own project, I've done some testing.
This is the testing code:
const
threadcount = 100;
function f(p : pointer) : ptrint;
begin
f:=0;
end;
procedure TForm1.BitBtnClick(Sender: TObject);
var
i : longint;
ThreadIDs : array[1..threadcount] of TThreadID;
startTime, totalTimeSeconds: double;
begin
startTime := Now();
for i:=1 to threadcount do
ThreadIDs[i] := BeginThread(@f,pointer(i));
for i:=1 to threadcount do
WaitForThreadTerminate( ThreadIDs[i],0 );
totalTimeSeconds := (Now() - startTime) * 24 * 60 * 60;
WriteLn(' Thread Time:', totalTimeSeconds:8:4,'s');
startTime := Now();
for i:=1 to threadcount do
f(pointer(i));
totalTimeSeconds := (Now() - startTime) * 24 * 60 * 60;
WriteLn(' Direct call:', totalTimeSeconds:8:4,'s');
end;
These are the results:
Thread Time: 0.1640s
Direct call: 0.0000s
Conclusion: in my computer, each thread consumes 1.6 milliseconds to be launched and waited to terminate.
This was tested in an old windows notebook. Wonder how it would perform in other systems. Would you like to give it a try?
Wish everyone happy coding!