Why is dictionary so much faster than list?

后端 未结 8 1955
盖世英雄少女心
盖世英雄少女心 2020-12-04 18:37

I am testing the speed of getting data from Dictionary VS list.
I\'ve used this code to test :

    internal class Program
{
    private static void Mai         


        
相关标签:
8条回答
  • 2020-12-04 19:34

    When it comes to lookup of data, a keyed collection is always faster than a non-keyed collection. This is because a non-keyed collection will have to enumerate its elements to find what you are looking for. While in a keyed collection you can just access the element directly via the key.

    These are some nice articles for comparing list to dictionary.

    Here. And this one.

    0 讨论(0)
  • 2020-12-04 19:37

    When using Dictionary you are using a key to retrieve your information, which enables it to find it more efficiently, with List you are using Single Linq expression, which since it is a list, has no other option other than to look in entire list for wanted the item.

    0 讨论(0)
提交回复
热议问题