Checking for the existence of an object in a collection (T)

I see that on this question LINQy's way to check if any objects in a collection have the same property value is a query to tell how to do something using LINQ to find out if a property in the collection is the same. However, is this the fastest sane process for doing this? I will be deploying something that requires some sort of resource management, and I want the application to be as responsive as it can be without making the code terribly difficult to decipher when someone else, or I'll come back to it again later.

+1


source to share


4 answers


However, is this the fastest sane process for doing this?

My guess is that a quick way (probably the fastest way) to do this might be to add all objects to a dictionary or HashSet using a property as a key field: a method like HashSet.Add has a return code that says to you if this property value has been added. For example:



static bool containsDuplicate(Container<Foo> fooCollection)
{
    //create the hash set
    HashSet<Bar> hashSet = new HashSet<Bar>();
    //for each object to be tested
    foreach (Foo foo in fooCollection)
    {
        //get the interesting object property
        Bar propertyValue = fooCollection.bar;
        //see whather we've already seen this property value
        if (!hashSet.Add(propertyValue))
        {
            //duplicate detected
            return true;
        }
    }
    //no duplicate detected
    return false;
}

      

+2


source


LINQ is almost never the fastest way (in terms of raw runtime) to do anything.

This is usually "fast enough". Once you have a working application with unit tests, you can review it to see if you need to optimize.



"We have to forget about a little efficiency, say about 97% of the time: premature optimization is the root of all evil." -Donald Knuth

+1


source


Indeed LINQ will work fine. Of course, if you know you can optimize the situation in this case, you can always write your own LINQ extension method for the more specific type. Since the typemore sepcific, your own method should be used in the default preference Enumerable

. Which is nice: -p

0


source


It really depends on the amount of data in your collection and the frequency at which this operation is performed. A Linq search for properties will have to read every item / property in the collection.

If your collection will only have 10 elements, and this operation is done once per second, then checking forward only to find an element by property is likely to be fast enough.

If you have 10 million items in your collection, then only forward or need to do this kind of operation 100 times per second, then you probably need some index for this property.

If it turns out that you need to index this, I would suggest encapsulating this logic into an object. So, for example, adding an item will add it to the main collection and add a property indexer to the sorts hash set.

0


source







All Articles