[Solved] Create Hashset with a large number of elements (1M)

[ad_1]

To build on @Enigmativity’s answer, here’s a proper benchmark using BenchmarkDotNet:

public class Benchmark
{
    private const int N = 1000000;

    [Benchmark]
    public HashSet<int> EnumerableRange() => new HashSet<int>(Enumerable.Range(1, N + 1));

    [Benchmark]
    public HashSet<int> NoPreallocation()
    {
        var result = new HashSet<int>();
        for (int n = 1; n < N + 1; n++)
        {
            result.Add(n);
        }
        return result;
    }

    [Benchmark]
    public HashSet<int> Preallocation()
    {
        var result = new HashSet<int>(N);
        for (int n = 1; n < N + 1; n++)
        {
            result.Add(n);
        }
        return result;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        BenchmarkRunner.Run(typeof(Program).Assembly);
    }
}

With the results:

MethodMeanErrorStdDev
EnumerableRange29.17 ms0.743 ms2.179 ms
NoPreallocation23.96 ms0.471 ms0.775 ms
Preallocation11.68 ms0.233 ms0.665 ms

As we can see, using linq is a bit slower than not using linq (as expected), and pre-allocating saves a significant amount of time.

1

[ad_2]

solved Create Hashset with a large number of elements (1M)