• 字典查找、linq、foreach、yield等几种查找性能对比


    先上代码,以1千万记录的内存查找测试:

     List<Student> stuList = new List<Student>();
                Dictionary<int, Student> dictStu = new Dictionary<int, Student>();
                Student student = null;
                for (int i = 0; i < 10000000; i++)
                {
                    student = new Student(i);
                    stuList.Add(student);
                    dictStu.Add(i, student);
                }
                Stopwatch sw = new Stopwatch();
                sw.Start();
                student = dictStu[99999];
                sw.Stop();
                Console.WriteLine(student.ToString());
                Console.WriteLine("dict={0}", sw.ElapsedMilliseconds);
    
    
                sw.Reset();
                sw.Start();
    
                 var yieldResult = StudentResult(stuList);
                 foreach (Student stu in yieldResult)
                 {
                     Console.WriteLine(stu.ToString());
                 }
                // Student s = yieldResult.First<Student>();
                //Console.WriteLine(s.ToString());
                sw.Stop();
    
                Console.WriteLine("yieldResult={0}", sw.ElapsedMilliseconds);
    
                sw.Reset();
                sw.Start();
    
                foreach (Student stu in stuList)
                {
                    if (stu.Age == 99999)
                    {
                        student = stu;
                        break;
                    }
                }
    
    
                sw.Stop();
    
                Console.WriteLine("foreach={0}", sw.ElapsedMilliseconds);
    
    
    
                sw.Reset();
                sw.Start();
    
                var result = from stu in stuList
                             where stu.Age == 99999
                             select stu;
    
                foreach (Student stu in result)
                {
                    Console.WriteLine(stu.ToString());
                }
                sw.Stop();
    
                Console.WriteLine("linq={0}", sw.ElapsedMilliseconds);
    View Code
        static IEnumerable<Student> StudentResult(List<Student> stuList)
            {
                foreach (Student stu in stuList)
                {
                    if (stu.Age == 99999)
                    {
                        yield return stu;
                    }
    
                }
            }
    yield 查找方式的方法定义

    执行结果

    结论:

    字典查找为哈希查找,性能最优,其次是foreach遍历,后依次为yield,linq

    //var result = from stu in stuList
    // //where stu.Age > 10 && stu.Age < 20
    // select stu;

    var result = stuList
    .Where(r => r.Age > 10 && r.Age < 20)
    .Select(r => r);

  • 相关阅读:
    如何解决git上传文件出错[rejected] master -> master (fetch first) error: failed to push some refs to '
    git
    pytest自动化测试执行环境切换
    JS实现菜单栏折叠
    vue-highlightjs 代码高亮
    C# 动态调用http及 webservice服务
    API接口优化的几个方面
    Leetcode__1508. Range Sum of Sorted Subarray Sums
    Batch Normalization 以及 Pytorch的实现
    Pytorch Transformer 中 Position Embedding 的实现
  • 原文地址:https://www.cnblogs.com/weiweictgu/p/5637017.html
Copyright © 2020-2023  润新知