HashSet <T> performance (compared to ObservableCollection <T>)?

I am currently working on a project where I have to manage large sets of unique elements. Each element has ~ 20 properties, and each element has a public DateTime property.

The DateTime property is not unique, so I cannot use a shared dictionary to store my data.

I am currently placing these items in an ObservableCollection, but the performance of deleting items from the collection is incredibly slow, I end up expecting ~ 20 seconds to remove ~ 7000 items from the collection of ~ 25,000 items.

(The search operation seems quite effective; it takes only ~ 30 ms to search 80 randomly selected items from an unsorted collection of 300,000 items).

Each element implements the GetHashCode () method, simply returning DateTime.GetHashCode ().

I thought using a HashSet, rather than an ObservableCollection, would increase my performance quite a bit, but this seems to have no effect ...

And using a generic dictionary is even worse ...

Is a HashSet more powerful than an ObservableCollection if the elements have "good" hash functions (very few elements that have the same hash code)?

+3
source share
3 answers

You need to override the Equals method of your objects.

HashSet IEqualityComparer, (null), " " , Equals:

class MyObject
{
    public Guid Id { get; set; }

    public override bool Equals(object other)
    {
        if (other is MyObject)
        {
            // use the 'Id' property as identifier

            MyObject myObj = (MyObject)obj;
            return myObj.Id == this.Id;
        }

        // is not a 'MyObject' based object
        return base.Equals(other);
    }
}

, .

EDIT:

, HashSet OberservableCollection. , (, , , ..) PropertyChanged CollectionChanged.

+3

ObservableCollection, . ItemCollection (BeginUpdate/EndUpdate):

  ItemCollection<Customer> customers = new ItemCollection<Customer>
  customers.BeginUpdate();
  customers.Add( new Customer( "Joe", "Smith" ) );
  customers.Add( new Customer( "Mary", "Jones" ) );
  customers.Add( new Customer( "Lisa", "Black" ) );
  customers.Add( new Customer( "Peter", "Brown" ) );
  customers.EndUpdate();

: XAML.

+2

Well Marcel's answer is correct, but if performance really matters, you can improve your equals method a bit:

class MyObject
{
    public Guid Id { get; set; }

    public override bool Equals(object other)
    {
        MyObject myObj = obj as MyObject;

        if (myObj != null)
        {
            // use the 'Id' property as identifier
            return myObj.Id == this.Id;
        }

        // is not a 'MyObject' based object
        return base.Equals(other);
    }
}

With this approach, you avoid an expensive function to check if an object is of type specialc two times, calling it only once and doing a quick zero check. For more information about this, you can take a look at this article from Eric .

+2
source

All Articles