先上代码,以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

相关文章:

  • 2022-02-07
  • 2021-10-26
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
猜你喜欢
  • 2022-02-01
  • 2021-11-27
  • 2022-12-23
  • 2021-05-06
  • 2021-08-05
  • 2022-12-23
相关资源
相似解决方案