Created
September 25, 2019 12:46
-
-
Save tigrouind/eb763f2d58214a6a33c6bfaca94c612f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
namespace Test | |
{ | |
class Program | |
{ | |
static Random rand = new Random(); | |
public static int[] GetArray() | |
{ | |
List<int> res = new List<int>(); | |
for(int i = 0 ; i < rand.Next(1, 4) ; i++) | |
{ | |
res.Add(rand.Next(1, 100)); | |
} | |
return res.ToArray(); | |
} | |
public static void Main() | |
{ | |
var items = Enumerable.Range(0, 5000) | |
.Select(x => new { Name = x.ToString(), Values = GetArray() }) | |
.ToList(); | |
Stopwatch sw = Stopwatch.StartNew(); | |
sw.Start(); | |
var tuples = items | |
.SelectMany(x => x.Values, (x, y) => new { x.Name, Value = y }) | |
.ToList(); | |
var result = tuples.Join(tuples, | |
x => x.Value, | |
x => x.Value, | |
(x, y) => new { NameA = x.Name, NameB = y.Name }) | |
.Where(x => x.NameA != x.NameB) | |
.Distinct() | |
.GroupBy(x => x.NameA, x => x.NameB, (x, y) => new { x, y = y.ToList() }) | |
.ToList(); | |
sw.Stop(); | |
Console.WriteLine(sw.Elapsed); | |
sw = Stopwatch.StartNew(); | |
var hashed = items | |
.Select(i => new { i.Name, Values = i.Values.ToHashSet() }) | |
.ToList(); | |
var overlaps = hashed.Select(h1 => new | |
{ | |
h1.Name, | |
Overlaps = hashed | |
.Where(h2 => h2.Name != h1.Name && h2.Values.Overlaps(h1.Values)) | |
.Select(h => h.Name) | |
.ToList() | |
}).ToList(); | |
Console.WriteLine(sw.Elapsed); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment