Question

i want to know which why is faster to build a hashset.

my process like this:

1, DB access (single thread) , get a large list of IDs.

2,
Plan A

foreach( var oneID in IDs)
{
    myHashSet.add(oneID);
}

Plan B

Parallel.ForEach(IDs,myPallOpt,(oneID)=>
{
    myHashSet.add(oneID);
});

So which is faster Plan A or B?

Thanks

Was it helpful?

Solution

HashSet<T> is not thread safe, so the second option (using Parallel.ForEach) will likely cause bugs. It should definitely be avoided.

The best option is likely to just build the hashset directly from the results:

var myHashSet = new HashSet<int>(IDs);

Note that this only works if the HashSet is only intended to contain the items from this collection. If you're adding to an existing HashSet<T>, a foreach (your first option) is likely the best option.

OTHER TIPS

Plan B likely won't work because it is likely not thread-safe (most .NET collection classes are not thread safe). You could fix it by making access to it thread-safe, but that essentially means serializing access to it, which is no better than single threaded. The only case it would make sense is if between beginning of your for loop and actual addition you have some cpu intensive processing that would be good to parallelize.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top