5

dotnet C# 字典 Dictionary 和 Hashtable 的性能对比

 3 years ago
source link: https://lindexi.gitee.io/post/dotnet-C-%E5%AD%97%E5%85%B8-Dictionary-%E5%92%8C-Hashtable-%E7%9A%84%E6%80%A7%E8%83%BD%E5%AF%B9%E6%AF%94.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
dotnet C# 字典 Dictionary 和 Hashtable 的性能对比

如果没有特别的需求,请使用 Dictionary 而不是 Hashtable 原因是 Dictionary 的性能更好,本文将告诉大家 Stephen Toub 大佬的评测

从 2021 的 6 月 23 日,在 WPF 仓库里面,开始看到了性能优化狂魔 Stephen Toub 大佬给 WPF 做的性能优化

如在 Use Dictionary instead of Hashtable in EventMap by stephentoub · Pull Request #4731 · dotnet/wpf 这里可以看到,他将使用 Dictionary 替换 Hashtable 类型用来做性能提升,同时也给出了性能评测

kXS16VgOjrLcqRi.jpg

大体来说就是 Hashtable 将会有额外的内存分配,如 Count 元素数量为 1 的时候,分配是 72B 的空间,同时在读写性能上,也不如字典来得快,性能差距大概是 10 倍左右。当哈希冲突大的时候,插入元素数量靠近分配的内存空间的时候,两者的性能差距将会从 10 倍逐步缩小为 5 倍的差距

以下是他的测试代码

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

[MemoryDiagnoser]
public class Program
{
    static void Main(string[] args) => BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);

    private readonly Hashtable _table = new Hashtable(20, .1f);
    private readonly Dictionary<int, object> _dictionary = new Dictionary<int, object>(20);
    private readonly object[] _objects = Enumerable.Range(0, 20).Select(_ => new object()).ToArray();

    [Params(1, 3, 5, 7, 9, 20)]
    public int Count { get; set; }

    [Benchmark]
    public void WithHashtable()
    {
        for (int i = 0; i < Count; i++)
        {
            _table.Add(i, _objects[i]);
        }
        for (int i = 0; i < Count; i++)
        {
            if (_table.ContainsKey(i))
            {
                object o = _table[i];
            }
        }
        _table.Clear();
    }

    [Benchmark]
    public void WithDictionary()
    {
        for (int i = 0; i < Count; i++)
        {
            _dictionary.Add(i, _objects[i]);
        }
        for (int i = 0; i < Count; i++)
        {
            if (_dictionary.TryGetValue(i, out object o))
            {
            }
        }
        _dictionary.Clear();
    }
}

以上代码可以从 github 看到,上面用了基准(标准)性能测试的方法,关于如何在 .NET 里面做基准性能测试,请看 C# 标准性能测试

此外在 WPF 仓库上,还有以下更改也是优化字典性能,其中还有我的更改


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/dotnet-C-%E5%AD%97%E5%85%B8-Dictionary-%E5%92%8C-Hashtable-%E7%9A%84%E6%80%A7%E8%83%BD%E5%AF%B9%E6%AF%94.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK