HashSet & List for storing Strings
<p>Good night,</p>

<p>I was trying to benchmark the performance of HashSet and List datatypes in C# to check which one performs best when a lot of insertions/deletes need to be done. The code I am using is roughly the following.</p>

<pre><code>public static Func&lt;String, HashSet&lt;String&gt;&gt; ListaPossiveisCorreccoes = StrPalavra =&gt; {

        (...)
        HashSet&lt;String&gt; ListaCorreccoes = new HashSet&lt;String&gt; ();
        (...)

        (Lots of .Add operations)

        return ListaCorreccoes;

    };


    public static Func&lt;String, IEnumerable&lt;String&gt;&gt; ListaCorreccoes = (StrPalavra) =&gt;
    {


        HashSet&lt;String&gt; ConjuntoCorreccoes = new HashSet&lt;String&gt; ( );

        foreach (String StrTmp in ListaPossiveisCorreccoes(StrPalavra))
            foreach (String StrTmp2 in ListaPossiveisCorreccoes(StrTmp))
                ConjuntoCorreccoes.Add(StrTmp2);

        return ConjuntoCorreccoes/*.Distinct()*/.Where(PalavraConhecida)*/;

    };
</code></pre>

<p>When I run sequentaly the two functions outlined above it takes about 250-285ms to complete (I am using memoization). However, if I replace <code>HashSet&lt;String&gt;</code> with <code>List&lt;String&gt;</code> everywhere and uncomment the commented part of the last line (thus forcing duplicates to be removed from the list) it takes only 140-145ms to complete. This contradicts what I read about HashSets, which says that their performance is much better than that of lists in addition/removal operations. Can someone please tell me if this is normal? 250ms isn't of course much, but it is almost twice the time taken with lists, and that is important since these functions are intended to be called repeatedly.</p>

<p>Thank you very much.</p>
