Recent

Author Topic: BeginThread WaitForThreadTerminate Benchmarking  (Read 967 times)

schuler

  • Full Member
  • ***
  • Posts: 230
BeginThread WaitForThreadTerminate Benchmarking
« on: May 20, 2018, 02:45:55 am »
 :) 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:

Code: Pascal  [Select][+][-]
  1. const
  2.   threadcount = 100;
  3.  
  4. function f(p : pointer) : ptrint;
  5. begin
  6.   f:=0;
  7. end;
  8.  
  9. procedure TForm1.BitBtnClick(Sender: TObject);
  10. var
  11.   i : longint;
  12.   ThreadIDs : array[1..threadcount] of TThreadID;
  13.   startTime, totalTimeSeconds: double;
  14. begin
  15.    startTime := Now();
  16.    for i:=1 to threadcount do
  17.      ThreadIDs[i] := BeginThread(@f,pointer(i));
  18.  
  19.    for i:=1 to threadcount do
  20.      WaitForThreadTerminate( ThreadIDs[i],0 );
  21.  
  22.    totalTimeSeconds := (Now() - startTime) * 24 * 60 * 60;
  23.  
  24.    WriteLn(' Thread Time:', totalTimeSeconds:8:4,'s');
  25.  
  26.    startTime := Now();
  27.    for i:=1 to threadcount do
  28.      f(pointer(i));
  29.  
  30.    totalTimeSeconds := (Now() - startTime) * 24 * 60 * 60;
  31.    WriteLn(' Direct call:', totalTimeSeconds:8:4,'s');
  32. end;
  33.  

These are the results:
Code: Pascal  [Select][+][-]
  1. Thread Time: 0.1640s
  2. 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! :)

 

TinyPortal © 2005-2018