RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1577348
Accepted
Kromster
Kromster
Asked:2024-04-23 02:55:43 +0000 UTC2024-04-23 02:55:43 +0000 UTC 2024-04-23 02:55:43 +0000 UTC

如何有效地从 Delphi 中的 TDictionary<key,value> 字典中删除大量对?

  • 772

我有一本德尔福词典,TDictionary<key,value>里面有几万对。值是带有字符串的记录。我需要根据某种条件一次性删除大约 10..90% 的对(通过按键,如果这很重要)。过滤器/条件预先未知。问题是如何有效地做到这一点(不浪费额外的时间和内存)?

例如,这是不正确的(在Delphi 11中),会留下额外的元素:

for var i in Dict.Keys do
 if i ... then
   Dict.Remove(i);

如果是TList<>,那么一切都很简单 - 我们从尾部迭代到头部Delete(I)(或者更好的是,使用第二个计数器,替换不必要的元素并在末尾切断)。

delphi
  • 2 2 个回答
  • 98 Views

2 个回答

  • Voted
  1. MBo
    2024-04-23T03:41:03Z2024-04-23T03:41:03Z

    创建一个包含 1300 万个元素的字典,花费了 5.1 秒。

    已删除 删除具有偶数键的元素 - 花费了 1.2 秒。似乎没有什么值得争的...

    删除是由 执行的function TDictionary<K,V>.DoRemove,在其代码中整个哈希表的铲除(例如永久数组移位)是不可见的。在删除帮助中:This is an O(1) operation

    已更改 -Dict.Keys.ToArray创建键数组的副本。

    var
      Dict: TDictionary<Integer, string>;
      i, j: Integer;
      t: DWord;
    begin
      Dict := TDictionary<Integer, string>.Create;
      Randomize;
      t := GetTickCount;
      for i := 1 to 20000000 do begin
        j := Random(20000000);
        if not Dict.ContainsKey(j) then
          Dict.Add(j, 'asdfasdf');
      end;
      Memo1.Lines.Add((GetTickCount - t).ToString + ' ' + Dict.Count.ToString);
      t := GetTickCount;
     
     {for i := 1 to 10000000 do
        if Dict.ContainsKey(i*2) then
           Dict.Remove(i*2); } 
    
     for i in Dict.Keys.ToArray do
       if i mod 2 = 0 then
          Dict.Remove(i);
    
      Memo1.Lines.Add((GetTickCount - t).ToString + ' ' + Dict.Count.ToString);
    

    输出到~i5-4440(时间以毫秒为单位,字典大小)

    5078 12662156
    1203 6330280
    

    输出最后一个片段的两次重复

    5485 12658135
    1390 6328059
    141 6328059
    141 6328059
    
    • 3
  2. Best Answer
    Kromster
    2024-04-25T18:01:02Z2024-04-25T18:01:02Z

    测试代码:

    procedure TForm1.FillDict;
    begin
      fDict.Clear;
      var t := GetTickCount;
      for var i := 1 to 3000000 do
      begin
        var tr: Integer;
        tr := i; //Random(3000000);
        fDict.TryAdd(tr, 'asdf'+IntToStr(i));
      end;
    end;
    
    procedure TForm1.TestRemove;
    begin
      var t := GetTickCount;
      for var i in fDict do
        if i.Key mod 2 = 0 then
          fDict.Remove(i.Key);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestRemoveKeys;
    begin
      var t := GetTickCount;
      for var i in fDict.Keys do
        if i mod 2 = 0 then
          fDict.Remove(i);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayAndRemove;
    begin
      var t := GetTickCount;
      var a := fDict.ToArray;
      for var i in a do
        if i.Key mod 2 = 0 then
          fDict.Remove(i.Key);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayInPlaceAndRemove;
    begin
      var t := GetTickCount;
      for var i in fDict.ToArray do
        if i.Key mod 2 = 0 then
          fDict.Remove(i.Key);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayKeysAndRemove;
    begin
      var t := GetTickCount;
      var a := fDict.Keys.ToArray;
      for var i in a do
        if i mod 2 = 0 then
          fDict.Remove(i);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayAndRemoveReverse;
    begin
      var t := GetTickCount;
      var a := fDict.ToArray;
      for var i := High(a) downto 0 do
        if a[i].Key mod 2 = 0 then
          fDict.Remove(a[I].Key);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayKeysAndRemoveReverse;
    begin
      var t := GetTickCount;
      var a := fDict.Keys.ToArray;
      for var i := High(a) downto 0 do
        if a[i] mod 2 = 0 then
          fDict.Remove(a[I]);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestToArrayClearAndReadd;
    begin
      var t := GetTickCount;
      var a := fDict.ToArray;
      fDict.Clear;
      for var i in a do
        if i.Key mod 2 = 0 then
          fDict.Add(i.Key, i.Value);
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    
    procedure TForm1.TestNewDict;
    begin
      var t := GetTickCount;
      var newDict := TDictionary<Integer, string>.Create;
      for var i in fDict do
        if i.Key mod 2 = 0 then
          newDict.Add(i.Key, i.Value);
      fDict.Free;
      fDict := newDict;
      Memo1.Lines.Add(Format('%d items left in %dms', [fDict.Count, GetTickCount - t]));
    end;
    

    以及 Delphi 11.3 在 i7-1355U 上调试的结果。经过多次运行测试,包括 无序 - 相对时间 +/- 相同。在 Release 中,一切都快一点,但相对值的分布方式相同:

    Remove
    1613196 items left in 234ms  <<<--- Остаются лишние элементы
    1503226 items left in 78ms
    ----------
    RemoveKeys
    1613196 items left in 219ms  <<<--- Остаются лишние элементы
    1503226 items left in 47ms
    ----------
    ToArrayAndRemove
    1500000 items left in 422ms
    1500000 items left in 140ms
    ----------
    ToArrayInPlaceAndRemove
    1500000 items left in 516ms
    1500000 items left in 141ms
    ----------
    ToArrayKeysAndRemove
    1500000 items left in 235ms   <<<--- Почти самый быстрый
    1500000 items left in 31ms
    ----------
    ToArrayAndRemoveReverse
    1500000 items left in 344ms
    1500000 items left in 109ms
    ----------
    ToArrayKeysAndRemoveReverse   <<<--- Самый быстрый
    1500000 items left in 218ms
    1500000 items left in 32ms
    ----------
    ToArrayAndReadd
    1500000 items left in 1125ms
    1500000 items left in 2032ms
    ----------
    NewDict
    1500000 items left in 1156ms
    1500000 items left in 2078ms
    
    • 2

相关问题

  • 查找数值

  • 如何在 DBGrid 中选择 DBDateTimeEditEh 编辑记录字段?

  • 该程序无法在 RAD STUDIO 10.3 中编译或运行

  • 我们需要一个可视化的 Delphi 组件,类似于在旧版本的 Windows 中显示磁盘碎片整理

  • 按钮代码 OnClick 通过另一个按钮

  • 这种代码排序方法是如何工作的?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5