#1 2015-02-24 14:46:02

nandofcgui
Member
Registered: 2015-02-23
Posts: 1

Performance issues into TDynArrayHashed using a Double key

I'm using the TDynArrayHashed to performe a series of batch comparisons, in my case i need to use an Double as key.

I've built an example demonstranting the diferences bettewen the inserction times using diferent types of record.

Integer key ----------------------------------------- (00:00:00 013)
11:27:27 200 : Adding values begin
11:27:27 213 : Adding values finish
11:27:27 213 : Capacity 1 : 100000
11:27:27 214 : Length     : 100000
11:27:27 214 : Count      : 99999
11:27:27 214 : Search begin
11:27:27 215 : Index not found : 0
11:27:27 217 : Index not found : 100000
11:27:27 218 : Search finish
String key ----------------------------------------- (00:00:00 086)
11:27:27 712 : Adding values begin
11:27:27 799 : Adding values finish (00:00:00 086)
11:27:27 800 : Capacity 1 : 100000
11:27:27 801 : Length     : 100000
11:27:27 802 : Count      : 99999
11:27:27 803 : Search begin
11:27:27 804 : Index not found : 0
11:27:27 874 : Index not found : 100000
11:27:27 875 : Search finish
Double key ----------------------------------------- (00:00:42 387)
11:27:28 721 : Adding values begin
11:28:11 109 : Adding values finish (00:00:42 387)
11:28:11 111 : Capacity 1 : 100000
11:28:11 112 : Length     : 100000
11:28:11 113 : Count      : 74203
11:28:11 114 : Search begin
11:28:11 116 : Index not found : 0
11:28:11 120 : Search end

Note that in Double case the Count is smaller than the expected and the structure has found a item that is not on the list.

Is there any another property or function to set to use the key as an double and obtain an better performance, i dont want to pay the Double conversion to string during the inserction and the validation.

Follow the code:

procedure TForm1.btnDoubleClick(Sender: TObject);

  type
    MyType = record
      Key : Double;
      Val : string;
  end;

  MyTypeDyn = array of MyType;

var
  DynArrayHashed : TDynArrayHashed;
  Ix   : Integer;
  Value  : MyType;
  Values : MyTypeDyn;
  CountT : integer;
  testVal : Double;

begin
  Memo1.Lines.Add('Double key -----------------------------------------');

  CountT := 0;

  DynArrayHashed.Init(TypeInfo(MyTypeDyn), Values, nil, nil, nil, @CountT);
  DynArrayHashed.Capacity := se1.Value;

  LogUpdate('Adding values begin');

  for Ix := 1 to se1.Value - 1 do
  begin
    testVal   := Ix;

    Value.Key := testVal;
    Value.Val := Int32ToUtf8(Ix);
    DynArrayHashed.FindHashedAndUpdate(Value, true);
  end;

  LogUpdate('Adding values finish');

  LogUpdate('Capacity 1 : '+IntToStr(DynArrayHashed.Capacity));
  LogUpdate('Length     : '+IntToStr(length(Values)));
  LogUpdate('Count      : '+IntToStr(countT));

  LogUpdate('Search begin');

  for Ix := 0 to se1.Value do
  begin
    testVal := Ix;

    if (DynArrayHashed.FindHashed( testVal) < 0) then
    begin
      LogUpdate('Index not found : '+ IntToStr(Ix));
    end;
  end;

  LogUpdate('Search end');

  DynArrayHashed.Clear;
end;

Offline

#2 2015-02-24 18:08:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Performance issues into TDynArrayHashed using a Double key

Double values are not hashable by design I am afraid...

Hashing is using the binary representation of the value, which may differ from the actual floating point value...

Try with currency if you can.

Offline

Board footer

Powered by FluxBB