Skip to content

Instantly share code, notes, and snippets.

@zaus
Last active June 30, 2017 16:22
Show Gist options
  • Save zaus/014ac9b5a78b267aa1643d63d30c7554 to your computer and use it in GitHub Desktop.
Save zaus/014ac9b5a78b267aa1643d63d30c7554 to your computer and use it in GitHub Desktop.

Revisions

  1. zaus revised this gist Jun 30, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Test-Collection-Contains.linq.cs
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,7 @@ void test(int setSize, int minWordLength = 5, int wordLengthVariance = 10, bool
    keys.Dump("Find");
    }

    // the thing that runs each test 10k times and prints the results, see
    // the thing that runs each test 10k times and prints the results, see https://gist.github.com/zaus/8055941
    new Perf {
    { "list, first", n => list.Contains(keys[0]) },
    { "hashset, first", n => hashset.Contains(keys[0]) },
  2. zaus created this gist Jun 30, 2017.
    81 changes: 81 additions & 0 deletions Test-Collection-Contains.linq.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    void Main()
    {
    // https://stackoverflow.com/questions/150750/hashset-vs-list-performance/13089134?noredirect=1#comment76605526_13089134

    test(10, 5, 10, true);
    test(20);
    test(50);
    test(100);
    test(1000);

    test(10, 50, 10, true);
    test(1000, 50);

    }

    // Define other methods and classes here
    static Random r = new Random();
    string getRandomString(int size) {
    var sb = new StringBuilder();
    for(int i = 0; i < size; i++) sb.Append( (char)('a' + r.Next(0, 25)));
    return sb.ToString();
    }

    ICollection<string> createTestList<T>(int setSize, int minWordLength = 5, int wordLengthVariance = 10, int previewSize = 0) where T : ICollection<string>, new() {
    var set = new T();
    for(int i = 0; i < setSize; i++) set.Add(getRandomString(r.Next(minWordLength, minWordLength + wordLengthVariance)));

    if(previewSize > 0) set.Take(previewSize).Dump(string.Format("Test size {0} sample {1}", setSize, previewSize));

    return set;
    }

    List<T> getSearchKeys<T>(ICollection<T> set) {
    return _getSearchKeys(set).ToList();
    }
    IEnumerable<T> _getSearchKeys<T>(ICollection<T> set) {
    // first
    yield return set.First();

    // early
    yield return set.ElementAt(set.Count / 10);

    // middle
    yield return set.ElementAt(set.Count / 2);

    // late
    yield return set.ElementAt(set.Count / 10 * 9 - 1);

    // last
    yield return set.Last();
    }

    void test(int setSize, int minWordLength = 5, int wordLengthVariance = 10, bool preview = false) {
    var list = createTestList<List<string>>(setSize, minWordLength, wordLengthVariance);
    var hashset = new HashSet<string>(list);

    var keys = getSearchKeys(list);

    if(preview) {
    list.Dump("Set");
    keys.Dump("Find");
    }

    // the thing that runs each test 10k times and prints the results, see
    new Perf {
    { "list, first", n => list.Contains(keys[0]) },
    { "hashset, first", n => hashset.Contains(keys[0]) },

    { "list, early", n => list.Contains(keys[1]) },
    { "hashset, early", n => hashset.Contains(keys[1]) },

    { "list, middle", n => list.Contains(keys[2]) },
    { "hashset, middle", n => hashset.Contains(keys[2]) },

    { "list, late", n => list.Contains(keys[3]) },
    { "hashset, late", n => hashset.Contains(keys[3]) },

    { "list, last", n => list.Contains(keys[4]) },
    { "hashset, last", n => hashset.Contains(keys[4]) },
    }.Vs(string.Format("Comparing set size {0} min word size {1}-{2}", setSize, minWordLength, minWordLength+wordLengthVariance));
    }