Advertisement
Guest User

Common list vs Hash list in Free Pascal

a guest
Feb 16th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.46 KB | None | 0 0
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, GHashMap;
  7.  
  8. type
  9.  
  10.   { TFCGIThread }
  11.  
  12.   TFCGIThread = class(TObject) // it is just a list example
  13.   private
  14.     FID: LongWord;
  15.     FName: string;
  16.   public
  17.     property ID: LongWord read FID write FID;
  18.     property Name: string read FName write FName;
  19.   end;
  20.  
  21.   { TFCGIThreadHash }
  22.  
  23.   TFCGIThreadHash = class(TObject)
  24.   public
  25.     class function Hash(A: SizeUInt; B: SizeUInt): SizeUInt;
  26.   end;
  27.  
  28.   { TFCGIThreadList }
  29.  
  30.   TFCGIThreadList = class(specialize THashmap<SizeUInt, TFCGIThread, TFCGIThreadHash>)
  31.   strict private class var
  32.     _CS: TRTLCriticalSection;
  33.   public
  34.     class constructor Create;
  35.     class destructor Destroy;
  36.     procedure Lock;
  37.     procedure Unlock;
  38.   end;
  39.  
  40.   { TFCGIThreadList }
  41.  
  42.   class constructor TFCGIThreadList.Create;
  43.   begin
  44.     InitCriticalSection(_CS);
  45.   end;
  46.  
  47.   class destructor TFCGIThreadList.Destroy;
  48.   begin
  49.     DoneCriticalsection(_CS);
  50.   end;
  51.  
  52.   procedure TFCGIThreadList.Lock;
  53.   begin
  54.     EnterCriticalsection(_CS);
  55.   end;
  56.  
  57.   procedure TFCGIThreadList.Unlock;
  58.   begin
  59.     LeaveCriticalsection(_CS);
  60.   end;
  61.  
  62.   { TFCGIThreadHash }
  63.  
  64.   class function TFCGIThreadHash.Hash(A: SizeUInt; B: SizeUInt): SizeUInt;
  65.   begin
  66.     Result := A mod B;
  67.   end;
  68.  
  69. const
  70.   Count = 150000;
  71. var
  72.   S: string;
  73.   _Out: string = '';
  74.   B, E: TDateTime;
  75.   I: Integer;
  76.   Item: TFCGIThread;
  77.   // commom
  78.   Thread: Pointer;
  79.   List: TList;
  80.   CommomList: TThreadList;
  81.   // hashset
  82.   HSList: TFCGIThreadList;
  83.   Iterator: TFCGIThreadList.TIterator;
  84. begin
  85.   // commom
  86.   B := Now;
  87.   CommomList := TThreadList.Create;
  88.   try
  89.     List := CommomList.LockList; // lock
  90.     for I := 1 to Count do
  91.     begin
  92.       Item := TFCGIThread.Create;
  93.       Item.Name := 'thread_' + IntToStr(I);
  94.       CommomList.Add(Item); // adding ...
  95.     end;
  96.     CommomList.UnlockList; // unlock
  97.  
  98.     List := CommomList.LockList; // lock
  99.     for Thread in List do
  100.     begin
  101.       S := TFCGIThread(Thread).Name; // iterating ...
  102.     end;
  103.     CommomList.UnlockList; // unlock
  104.  
  105.     List := CommomList.LockList; // lock
  106.     for Thread in List do
  107.       TObject(Thread).Free; // deleting ...
  108.     CommomList.UnlockList; // unlock
  109.   finally
  110.     CommomList.Free;
  111.   end;
  112.   E := Now;
  113.   _Out := 'Commom: ' + FormatDateTime('hh:nn:ss.zzz', E - B);
  114.  
  115.   // hashset
  116.   B := Now;
  117.   HSList := TFCGIThreadList.Create;
  118.   try
  119.     HSList.Lock; // lock
  120.     for I := 1 to Count do
  121.     begin
  122.       Item := TFCGIThread.Create;
  123.       Item.Name := 'thread_' + IntToStr(I);
  124.       HSList.Insert(I { thread ID }, Item); // adding ...
  125.     end;
  126.     HSList.Unlock; // unlock
  127.  
  128.     HSList.Lock; // lock
  129.     Iterator := HSList.Iterator;
  130.     repeat
  131.       S := Iterator.Value.Name; // iterating ... (you can use the fast 'Contains' method)
  132.     until not Iterator.Next;
  133.     // Don't forget to destroy iterator, use try..finally in your project!
  134.     Iterator.Destroy;
  135.     HSList.Unlock; // unlock
  136.  
  137.     HSList.Lock; // lock
  138.     Iterator := HSList.Iterator;
  139.     repeat
  140.       Iterator.Value.Free; // deleting ...
  141.     until not Iterator.Next;
  142.     Iterator.Destroy;
  143.     HSList.FDataSize := 0;
  144.     HSList.Unlock; // unlock
  145.   finally
  146.     HSList.Free;
  147.   end;
  148.   E := Now;
  149.   _Out := _Out + ' vs Hash: ' + FormatDateTime('hh:nn:ss.zzz', E - B);
  150.  
  151.   // in my PC (FPC from fixes_3_0, Win 7 64 bits, CPU i7, RAM 6 GB, HD SSD):
  152.   // Commom: 00:00:07.554 vs Hash: 00:00:00.109
  153.   WriteLn(_Out);
  154.  
  155.   ReadLn;
  156. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement