Guest User

Untitled

a guest
Apr 19th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.65 KB | None | 0 0
  1. unit rawUTF8ListUnit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, Graphics, Controls, Forms, Dialogs, StdCtrls,
  7.   SysUtils, Variants, Classes {$ifdef UNICODE}, system.Generics.Collections{$endif};
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     mmo1: TMemo;
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. uses
  25.   SynCommons;
  26.  
  27. {$R *.dfm}
  28.  
  29. procedure TForm1.FormCreate(Sender: TObject);
  30. var
  31.   list: TRawUTF8ListHashed;
  32.   locked: TRawUTF8ListHashedLocked;
  33.   i, n: integer;
  34.   timer: TPrecisionTimer;
  35.   {$ifdef UNICODE}
  36.   dict: TObjectDictionary<RawUTF8, TObject>;
  37.   {$endif}
  38.   sd: TSynDictionary;
  39.   s: RawUTF8;
  40.   str: array of RawUTF8;
  41.   v: TObject;
  42. begin
  43.   n := 2000000;
  44.   SetLength(str, n);
  45.   for i := 0 to n - 1 do
  46.     UInt32ToUTF8(i, str[i]);
  47.  
  48.   list := TRawUTF8ListHashed.Create;
  49.   try
  50.     timer.Start;
  51.     for i := 0 to n - 1 do
  52.       list.AddObject(str[i], pointer(i));
  53.     s := FormatUTF8('% TRawUTF8ListHashed.AddObject in % (%/s)', [n, timer.Stop, timer.PerSec(n)]);
  54.     timer.Start;
  55.     list.ReHash;
  56.     s := FormatUTF8('%'#13#10'TRawUTF8ListHashed.Rehash in %', [s, timer.Stop]);
  57.     timer.Start;
  58.     list.Clear;
  59.     s := FormatUTF8('%'#13#10'TRawUTF8ListHashed.Clear in %', [s, timer.Stop]);
  60.     timer.Start;
  61.     for i := 0 to n - 1 do
  62.       list.AddObjectIfNotExisting(str[i], pointer(i));
  63.     s := FormatUTF8('%'#13#10'% TRawUTF8ListHashed.AddObjectIfNotExisting in % (%/s)',
  64.       [s, n, timer.Stop, timer.PerSec(n)]);
  65.     timer.Start;
  66.     list.IndexOf('1000');
  67.     s := FormatUTF8('%'#13#10'first TRawUTF8ListHashed.IndexOf in %', [s, timer.Stop]);
  68.     timer.Start;
  69.     for i := 0 to n - 1 do
  70.       list.IndexOf(str[i]);
  71.     s := FormatUTF8('%'#13#10'% TRawUTF8ListHashed.IndexOf in % (%/s)', [s, n, timer.Stop, timer.PerSec(n)]);
  72.     mmo1.Lines.Text := UTF8ToString(s);
  73.   finally
  74.     list.Free;
  75.   end;
  76.  
  77.   locked := TRawUTF8ListHashedLocked.Create;
  78.   try
  79.     timer.Start;
  80.     for i := 0 to n - 1 do begin
  81.       locked.Safe.Lock;
  82.       try
  83.         locked.AddObject(str[i], pointer(i));
  84.       finally
  85.         locked.Safe.UnLock;
  86.       end;
  87.     end;
  88.     s := FormatUTF8('% TRawUTF8ListHashedLocked.AddObject in % (%/s)', [n, timer.Stop, timer.PerSec(n)]);
  89.     timer.Start;
  90.     locked.ReHash;
  91.     s := FormatUTF8('%'#13#10'TRawUTF8ListHashedLocked.Rehash in %', [s, timer.Stop]);
  92.     timer.Start;
  93.     locked.Clear;
  94.     s := FormatUTF8('%'#13#10'TRawUTF8ListHashedLocked.Clear in %', [s, timer.Stop]);
  95.     timer.Start;
  96.     for i := 0 to n - 1 do
  97.       locked.AddObjectIfNotExisting(str[i], pointer(i));
  98.     s := FormatUTF8('%'#13#10'% TRawUTF8ListHashedLocked.AddObjectIfNotExisting in % (%/s)',
  99.       [s, n, timer.Stop, timer.PerSec(n)]);
  100.     timer.Start;
  101.     locked.LockedGetObjectByName('1000');
  102.     s := FormatUTF8('%'#13#10'first TRawUTF8ListHashedLocked.LockedGetObjectByName in %', [s, timer.Stop]);
  103.     timer.Start;
  104.     for i := 0 to n - 1 do
  105.       locked.LockedGetObjectByName(str[i]);
  106.     s := FormatUTF8('%'#13#10'% TRawUTF8ListHashedLocked.LockedGetObjectByName in % (%/s)', [s, n, timer.Stop, timer.PerSec(n)]);
  107.     mmo1.Lines.Text := mmo1.Lines.Text + #13#10#13#10 + UTF8ToString(s);
  108.   finally
  109.     locked.Free;
  110.   end;
  111.  
  112.   sd := TSynDictionary.Create(TypeInfo(TRawUTF8DynArray), TypeInfo(TPointerDynArray));
  113.   try
  114.     sd.Capacity := n;
  115.     timer.Start;
  116.     for i := 0 to n - 1 do
  117.       sd.Add(str[i], pointer(i));
  118.     s := FormatUTF8('% TSynDictionary.Add in % (%/s)', [n, timer.Stop, timer.PerSec(n)]);
  119.     timer.Start;
  120.     sd.FindAndCopy(str[10], v);
  121.     s := FormatUTF8('%'#13#10'first TSynDictionary.FindAndCopy in %', [s, timer.Stop]);
  122.     timer.Start;
  123.     for i := 0 to n - 1 do
  124.       sd.FindAndCopy(str[i], v);
  125.     s := FormatUTF8('%'#13#10'% TSynDictionary.FindAndCopy in % (%/s)', [s, n, timer.Stop, timer.PerSec(n)]);
  126.     mmo1.Lines.Text := mmo1.Lines.Text + #13#10#13#10 + UTF8ToString(s);
  127.   finally
  128.     sd.Free;
  129.   end;
  130.  
  131.   {$ifdef UNICODE}
  132.   dict := TObjectDictionary<RawUTF8, TObject>.Create([]);
  133.   try
  134.     timer.Start;
  135.     for i := 0 to n - 1 do
  136.       dict.Add(str[i], pointer(i));
  137.     s := FormatUTF8('% TObjectDictionary.Add in % (%/s)', [n, timer.Stop, timer.PerSec(n)]);
  138.     timer.Start;
  139.     dict.TryGetValue('1000', v);
  140.     s := FormatUTF8('%'#13#10'first TObjectDictionary.TryGetValue in %', [s, timer.Stop]);
  141.     timer.Start;
  142.     for i := 0 to n - 1 do
  143.       dict.TryGetValue(str[i], v);
  144.     s := FormatUTF8('%'#13#10'% TObjectDictionary.TryGetValue in % (%/s)', [s, n, timer.Stop, timer.PerSec(n)]);
  145.     mmo1.Lines.Text := mmo1.Lines.Text + #13#10#13#10 + UTF8ToString(s);
  146.   finally
  147.     dict.Free;
  148.   end;
  149.  
  150.   dict := TObjectDictionary<RawUTF8, TObject>.Create([]);
  151.   try
  152.     timer.Start;
  153.     for i := 0 to n - 1 do begin
  154.       System.TMonitor.Enter(dict);
  155.       try
  156.         dict.Add(str[i], pointer(i));
  157.       finally
  158.         System.TMonitor.Exit(dict);
  159.       end;
  160.     end;
  161.     s := FormatUTF8('% TObjectDictionary.Add locked in % (%/s)', [n, timer.Stop, timer.PerSec(n)]);
  162.     timer.Start;
  163.     dict.TryGetValue('1000', v);
  164.     s := FormatUTF8('%'#13#10'first TObjectDictionary.TryGetValue in %', [s, timer.Stop]);
  165.     timer.Start;
  166.     for i := 0 to n - 1 do begin
  167.       System.TMonitor.Enter(dict);
  168.       try
  169.         dict.TryGetValue(str[i], v);
  170.       finally
  171.         System.TMonitor.Exit(dict);
  172.       end;
  173.     end;
  174.     s := FormatUTF8('%'#13#10'% TObjectDictionary.TryGetValue locked in % (%/s)', [s, n, timer.Stop, timer.PerSec(n)]);
  175.     mmo1.Lines.Text := mmo1.Lines.Text + #13#10#13#10 + UTF8ToString(s);
  176.   finally
  177.     dict.Free;
  178.   end;
  179.   {$endif}
  180. end;
  181.  
  182. end.
Advertisement
Add Comment
Please, Sign In to add comment