Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program project1;
- {$mode objfpc}{$H+}
- uses
- Classes, SysUtils, GHashMap;
- type
- { TFCGIThread }
- TFCGIThread = class(TObject) // it is just a list example
- private
- FID: LongWord;
- FName: string;
- public
- property ID: LongWord read FID write FID;
- property Name: string read FName write FName;
- end;
- { TFCGIThreadHash }
- TFCGIThreadHash = class(TObject)
- public
- class function Hash(A: SizeUInt; B: SizeUInt): SizeUInt;
- end;
- { TFCGIThreadList }
- TFCGIThreadList = class(specialize THashmap<SizeUInt, TFCGIThread, TFCGIThreadHash>)
- strict private class var
- _CS: TRTLCriticalSection;
- public
- class constructor Create;
- class destructor Destroy;
- procedure Lock;
- procedure Unlock;
- end;
- { TFCGIThreadList }
- class constructor TFCGIThreadList.Create;
- begin
- InitCriticalSection(_CS);
- end;
- class destructor TFCGIThreadList.Destroy;
- begin
- DoneCriticalsection(_CS);
- end;
- procedure TFCGIThreadList.Lock;
- begin
- EnterCriticalsection(_CS);
- end;
- procedure TFCGIThreadList.Unlock;
- begin
- LeaveCriticalsection(_CS);
- end;
- { TFCGIThreadHash }
- class function TFCGIThreadHash.Hash(A: SizeUInt; B: SizeUInt): SizeUInt;
- begin
- Result := A mod B;
- end;
- const
- Count = 150000;
- var
- S: string;
- _Out: string = '';
- B, E: TDateTime;
- I: Integer;
- Item: TFCGIThread;
- // commom
- Thread: Pointer;
- List: TList;
- CommomList: TThreadList;
- // hashset
- HSList: TFCGIThreadList;
- Iterator: TFCGIThreadList.TIterator;
- begin
- // commom
- B := Now;
- CommomList := TThreadList.Create;
- try
- List := CommomList.LockList; // lock
- for I := 1 to Count do
- begin
- Item := TFCGIThread.Create;
- Item.Name := 'thread_' + IntToStr(I);
- CommomList.Add(Item); // adding ...
- end;
- CommomList.UnlockList; // unlock
- List := CommomList.LockList; // lock
- for Thread in List do
- begin
- S := TFCGIThread(Thread).Name; // iterating ...
- end;
- CommomList.UnlockList; // unlock
- List := CommomList.LockList; // lock
- for Thread in List do
- TObject(Thread).Free; // deleting ...
- CommomList.UnlockList; // unlock
- finally
- CommomList.Free;
- end;
- E := Now;
- _Out := 'Commom: ' + FormatDateTime('hh:nn:ss.zzz', E - B);
- // hashset
- B := Now;
- HSList := TFCGIThreadList.Create;
- try
- HSList.Lock; // lock
- for I := 1 to Count do
- begin
- Item := TFCGIThread.Create;
- Item.Name := 'thread_' + IntToStr(I);
- HSList.Insert(I { thread ID }, Item); // adding ...
- end;
- HSList.Unlock; // unlock
- HSList.Lock; // lock
- Iterator := HSList.Iterator;
- repeat
- S := Iterator.Value.Name; // iterating ... (you can use the fast 'Contains' method)
- until not Iterator.Next;
- // Don't forget to destroy iterator, use try..finally in your project!
- Iterator.Destroy;
- HSList.Unlock; // unlock
- HSList.Lock; // lock
- Iterator := HSList.Iterator;
- repeat
- Iterator.Value.Free; // deleting ...
- until not Iterator.Next;
- Iterator.Destroy;
- HSList.FDataSize := 0;
- HSList.Unlock; // unlock
- finally
- HSList.Free;
- end;
- E := Now;
- _Out := _Out + ' vs Hash: ' + FormatDateTime('hh:nn:ss.zzz', E - B);
- // in my PC (FPC from fixes_3_0, Win 7 64 bits, CPU i7, RAM 6 GB, HD SSD):
- // Commom: 00:00:07.554 vs Hash: 00:00:00.109
- WriteLn(_Out);
- ReadLn;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement