locked
Dictionary, C# - To check is worth already present? RRS feed

  • Question

  • Hello,

    Contains is slow, how do you better test it?

    The spelling looks more complicated.
    Can someone please show me, how I would have to change the lower code to use trygetvalue?
    Dictionary<int, List<int>> dictionaryListIndices;
    dictionaryListIndices = new Dictionary<int, List<int>>();
    dictionaryListIndices.Add(1, new List<int>());
    dictionaryListIndices.Add(2, new List<int>());
    ...
     foreach (Product.POSITION forIndex in checkAsysProduct.PRODUCTLAYOUT.SIDE1.POSITIONS.ListPOSITIONS)
                        {
                            if (!dictionaryListIndices[1].Contains(forIndex.INDEX.value))
                                dictionaryListIndices[1].Add(forIndex.INDEX.value);
                        }


    Regards Markus

    Tuesday, April 24, 2018 5:01 PM

Answers

  • In some circumstances, a set (HashSet or SortedSet) can be used instead of List:

    Dictionary<int, HashSet<int>> dictionaryListIndices;

    dictionaryListIndices = new Dictionary<int, HashSet<int>>();

    dictionaryListIndices.Add( 1, new HashSet<int>() );

    dictionaryListIndices.Add( 2, new HashSet<int>() );

    //. . .

    var s = dictionaryListIndices[1];

    foreach( Product.POSITION forIndex in checkAsysProduct.PRODUCTLAYOUT.SIDE1.POSITIONS.ListPOSITIONS )

    {

                    s.Add( forIndex.INDEX.value );

    }


    • Edited by Viorel_MVP Tuesday, April 24, 2018 5:36 PM
    • Proposed as answer by Fei Hu Wednesday, April 25, 2018 3:19 AM
    • Marked as answer by Markus Freitag Saturday, April 28, 2018 10:16 AM
    Tuesday, April 24, 2018 5:36 PM
  • List.Contains requires that the list be enumerated until the item is found. You aren't going to be able to speed that up short of using a dictionary. If you expect each item in the list to be unique then don't use a List<T>, consider using a HashSet instead. It doesn't guarantee ordering but doesn't allow duplicates. But you'd probably need to be dealing with a very large set of data before you're going to notice a huge performance difference. Enumerating a list simply doesn't take that much time since it is just reading memory.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Markus Freitag Saturday, April 28, 2018 10:16 AM
    Tuesday, April 24, 2018 5:48 PM

All replies

  • In some circumstances, a set (HashSet or SortedSet) can be used instead of List:

    Dictionary<int, HashSet<int>> dictionaryListIndices;

    dictionaryListIndices = new Dictionary<int, HashSet<int>>();

    dictionaryListIndices.Add( 1, new HashSet<int>() );

    dictionaryListIndices.Add( 2, new HashSet<int>() );

    //. . .

    var s = dictionaryListIndices[1];

    foreach( Product.POSITION forIndex in checkAsysProduct.PRODUCTLAYOUT.SIDE1.POSITIONS.ListPOSITIONS )

    {

                    s.Add( forIndex.INDEX.value );

    }


    • Edited by Viorel_MVP Tuesday, April 24, 2018 5:36 PM
    • Proposed as answer by Fei Hu Wednesday, April 25, 2018 3:19 AM
    • Marked as answer by Markus Freitag Saturday, April 28, 2018 10:16 AM
    Tuesday, April 24, 2018 5:36 PM
  • List.Contains requires that the list be enumerated until the item is found. You aren't going to be able to speed that up short of using a dictionary. If you expect each item in the list to be unique then don't use a List<T>, consider using a HashSet instead. It doesn't guarantee ordering but doesn't allow duplicates. But you'd probably need to be dealing with a very large set of data before you're going to notice a huge performance difference. Enumerating a list simply doesn't take that much time since it is just reading memory.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Markus Freitag Saturday, April 28, 2018 10:16 AM
    Tuesday, April 24, 2018 5:48 PM