Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AreCollectionsSame #814

Open
Lonli-Lokli opened this issue Apr 12, 2021 · 3 comments
Open

Add AreCollectionsSame #814

Lonli-Lokli opened this issue Apr 12, 2021 · 3 comments

Comments

@Lonli-Lokli
Copy link

It's pretty useful and widely used feature, to check if two unordered collections are the same, probably with default and nondefault comparers

@leandromoh
Copy link
Collaborator

leandromoh commented May 22, 2021

you can easily create an extension method that does something like

var seq1 = list1.OrderBy(t => t);
var seq2 = list2.OrderBy(t => t);
return seq1.SequenceEqual(seq2);

or maybe

var set1 = list1.ToHashSet();
var set2 = lits2.ToHashSet();
return set1.SetEquals(set2);

@Lonli-Lokli
Copy link
Author

Lonli-Lokli commented May 22, 2021

@leandromoh I understand that I can, I actually created test for myself. I was just hoping to see the most efficient way in this library available for everyone.

|           Method |       Size |           Mean |        Error |       StdDev |         Median |
|----------------- |----------- |---------------:|-------------:|-------------:|---------------:|
|  SortThenCompare |    Size 10 |       766.6 ns |     22.31 ns |     63.28 ns |       748.9 ns |
| ConvertToHashset |    Size 10 |       624.6 ns |     10.01 ns |      9.36 ns |       623.3 ns |
| OneExceptAnother |    Size 10 |       871.5 ns |      9.11 ns |      8.52 ns |       872.0 ns |
|  SortThenCompare |   Size 100 |    10,697.1 ns |    188.36 ns |    334.82 ns |    10,578.2 ns |
| ConvertToHashset |   Size 100 |     3,730.6 ns |     74.27 ns |    151.71 ns |     3,661.1 ns |
| OneExceptAnother |   Size 100 |     6,319.3 ns |    115.18 ns |    102.10 ns |     6,320.1 ns |
|  SortThenCompare |  Size 1000 |   179,022.0 ns |  2,306.86 ns |  2,157.84 ns |   178,539.6 ns |
| ConvertToHashset |  Size 1000 |    36,351.3 ns |    721.78 ns |  1,951.38 ns |    35,832.8 ns |
| OneExceptAnother |  Size 1000 |    58,811.9 ns |    843.54 ns |    789.05 ns |    59,027.8 ns |
|  SortThenCompare | Size 10000 | 2,428,770.1 ns | 47,772.12 ns | 81,120.69 ns | 2,397,744.7 ns |
| ConvertToHashset | Size 10000 |   345,496.7 ns |  6,631.58 ns | 10,518.39 ns |   344,402.7 ns |
| OneExceptAnother | Size 10000 |   598,397.5 ns | 11,831.15 ns | 16,967.88 ns |   599,364.8 ns |
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool SortThenCompare(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return x.OrderBy(t => t).SequenceEqual(y.OrderBy(l => l));
        }
        
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool ConvertToHashset(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return x.ToHashSet().SetEquals(y.ToHashSet());
        }
        
        [Benchmark]
        [ArgumentsSource(nameof(Datasource))]
        public bool OneExceptAnother(TestSource source)
        {
            var x = source.XValues;
            var y = source.YValues;

            return !x.Except(y).Any() && !y.Except(x).Any();
        }

@viceroypenguin
Copy link
Contributor

@Lonli-Lokli This has been implemented in SuperLinq, and will be released with v4.1.0 in the next few weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants