C# Linq.FirstOrDefault、Linq.Where、Linq.AsParallel、List.Exists、List.Find、Dictionar.TryGetValue、HashSet.Contains 性能的比较

今天我们来比较一下集合检索方法性能更优问题,测试代码

复制代码
public class Entity
{
    public int Id { get; set; }
    public int No { get; set; }
    public string Col1 { get; set; }
    public string Col2 { get; set; }
    public string Col3 { get; set; }
    public string Col4 { get; set; }
    public string Col5 { get; set; }
    public string Col6 { get; set; }
    public string Col7 { get; set; }
    public string Col8 { get; set; }
    public string Col9 { get; set; }
    public string Col10 { get; set; }
}
static void TestFindVelocity(int totalDataCount, int executeCount)
{
    #region 构造数据
    List<Entity> datas = new List<Entity>();
    for (int i = 0; i < totalDataCount; i++)
    {
        var item = new Entity
        {
            No = i + 1,
            Col1 = Guid.NewGuid().ToString("N"),
            Col2 = Guid.NewGuid().ToString("N"),
            Col3 = Guid.NewGuid().ToString("N"),
            Col4 = Guid.NewGuid().ToString("N"),
            Col5 = Guid.NewGuid().ToString("N"),
            Col6 = Guid.NewGuid().ToString("N"),
            Col7 = Guid.NewGuid().ToString("N"),
            Col8 = Guid.NewGuid().ToString("N"),
            Col9 = Guid.NewGuid().ToString("N"),
            Col10 = Guid.NewGuid().ToString("N"),
        };
        datas.Add(item);
    }
    #endregion
    var dicDatas = datas.ToDictionary(x => x.No);
    var hashSetDatas = datas.ConvertAll<Tuple<int, int>>(x => new Tuple<int, int>(x.No, x.No + 1000)).ToHashSet();
    Stopwatch sw = new Stopwatch();
    Random random = new Random();
    Entity searchResult = null;
    bool searchResultBool = false;
    // 每次查询索引
    List<int> indexs = Enumerable.Range(1, executeCount).Select(x => random.Next(1, totalDataCount)).ToList();

    sw.Start();
    for (int i = 0; i < executeCount; i++)
    {
        searchResult = datas.FirstOrDefault(x => x.No == indexs[i]);
    }
    sw.Stop();
    Console.WriteLine($"list FirstOrDefault 耗时:{sw.ElapsedMilliseconds}");

    sw.Restart();
    for (int i = 0; i < executeCount; i++)
    {
        searchResult = datas.Where(x => x.No == indexs[i]).First();
    }
    sw.Stop();
    Console.WriteLine($"list Where+First 耗时:{sw.ElapsedMilliseconds}");

    sw.Restart();
    for (int i = 0; i < executeCount; i++)
    {
        searchResultBool = datas.Exists(x => x.No == indexs[i]);
    }
    sw.Stop();
    Console.WriteLine($"list Exist 耗时:{sw.ElapsedMilliseconds}");

    sw.Restart();
    for (int i = 0; i < executeCount; i++)
    {
        searchResult = datas.Find(x => x.No == indexs[i]);
    }
    sw.Stop();
    Console.WriteLine($"list Find 耗时:{sw.ElapsedMilliseconds}");

    sw.Restart();
    for (int i = 0; i < executeCount; i++)
    {
        dicDatas.TryGetValue(indexs[i], out searchResult);
    }
    sw.Stop();
    Console.WriteLine($"dictionary TryGetValue 耗时:{sw.ElapsedMilliseconds}");

    sw.Restart();
    for (int i = 0; i < executeCount; i++)
    {
        searchResultBool = hashSetDatas.Contains(new Tuple<int, int>(indexs[i], indexs[i] + 1000));
    }
    sw.Stop();
    Console.WriteLine($"Hashset contains 耗时:{sw.ElapsedMilliseconds}");
}
复制代码

结果

(集合数量,测试次数) Linq.FirstOrDefault Linq.Where+First
List.Exists
List.Find
Dictionary.TryGetValue
HashSet.Contains

(100, 5000000)

4544 3521 1992 1872 66 924

(1000, 5000000)

41751 29417 20631 19490 70 869

(10000, 5000000)

466918 397425 276409 281647 85 946

(50000, 5000)

6292 4602 4252 3559 0 2

(500000, 5000)

56988 55568 48423 48395 1 5
posted @   Karl_Albright  阅读(434)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示