我正在测试从字典和列表中获取数据的速度.

    internal class Program
{
    private static void Main(string[] args)
    {
        var stopwatch = new Stopwatch();
        List<Grade> grades = Grade.GetData().ToList();
        List<Student> students = Student.GetStudents().ToList();

        stopwatch.Start();
        foreach (Student student in students)
        {
            student.Grade = grades.Single(x => x.StudentId == student.Id).Value;
        }
        stopwatch.Stop();
        Console.WriteLine("Using list {0}", stopwatch.Elapsed);
        stopwatch.Reset();
        students = Student.GetStudents().ToList();
        stopwatch.Start();
        Dictionary<Guid, string> dic = Grade.GetData().ToDictionary(x => x.StudentId, x => x.Value);
        foreach (Student student in students)
        {
            student.Grade = dic[student.Id];
        }
        stopwatch.Stop();
        Console.WriteLine("Using dictionary {0}", stopwatch.Elapsed);
        Console.ReadKey();
    }
}

public class GuidHelper
{
    public static List<Guid> ListOfIds=new List<Guid>();

    static GuidHelper()
    {
        for (int i = 0; i < 10000; i++)
        {
            ListOfIds.Add(Guid.NewGuid());
        }
    }
}


public class Grade
{
    public Guid StudentId { get; set; }
    public string Value { get; set; }

    public static IEnumerable<Grade> GetData()
    {
        for (int i = 0; i < 10000; i++)
        {
            yield return new Grade
                             {
                                 StudentId = GuidHelper.ListOfIds[i], Value = "Value " + i
                             };
        }
    }
}

public class Student
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Grade { get; set; }

    public static IEnumerable<Student> GetStudents()
    {
        for (int i = 0; i < 10000; i++)
        {
            yield return new Student
                             {
                                 Id = GuidHelper.ListOfIds[i],
                                 Name = "Name " + i
                             };
        }
    }
}

记忆中有他们共同的学生名单和成绩

推荐答案

执行此操作时:

student.Grade = grades.Single(x => x.StudentId == student.Id).Value;

如上所述,它必须枚举整个List,直到在列表中找到具有正确的StudentId的条目(条目0是否与lambda匹配?不是的...条目1是否与λ匹配?不是的...等等).这是O(N).因为你对每个学生做一次,所以是O(n^2).

但是,当您执行此操作时:

student.Grade = dic[student.Id];

如果你想在字典中通过键找到某个元素,它可以立即跳转到它在字典中的位置——这是O(1).感谢 for each 学生做这件事.(如果你想知道这是如何实现的——Dictionary对键进行数学运算,将其转换为一个值,该值位于Dictionary中,与插入时的位置相同)

因此,字典速度更快,因为您使用了更好的算法.

.net相关问答推荐

Docker镜像mcr.microsoft.com/dotnet/aspnet:8.0不能在Windows上构建

.NET最小API BadRequest响应不返回正文

EGC / 文本元素上的 .NET String.Split

在 WP7 中将 List 转换为 ObservableCollection

移位比Java中的乘法和除法更快吗? .网?

为 XML 编码文本数据的最佳方法

如何获取 Sql Server 数据库中所有模式的列表

如何将浮点数向上舍入到 C# 中最近的 int?

抛出 ArgumentNullException

C# 有办法给我一个不可变的字典吗?

注册 COM 互操作与使程序集 COM 可见

C# 的 Actors 有什么好的实现吗?

寻找 .NET 的命令行参数解析器

我不了解应用程序域

是否有可用的 WPF 备忘单?

dotnet 恢复警告 NU1701

资源(.resx)文件有什么好处?

获取系统中已安装的应用程序

通过继承扩展枚举

作者主签名的时间戳发现了一个建链问题:UntrustedRoot: self-signed certificate in certificate chain