Contrast of two sets

There is a project on GitHub, which was originally the Demo of my own research and study. net core. Unexpectedly, many students were watching it and gave it to many stars. So I think it should be promoted to 3.0, collated, blogged and shared with the students of learning. net core.

Project name: Asp. NetCore Experiment

Project address: https://github.com/axzxs2001/Asp.NetCoreExperiment

 

Today we will share a few pairs of knowledge points.

Two sets of comparison codes:

GitHub Code Library( https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Common/CompareCollections)

 1         static void Main(string[] args)
 2         {
 3             var leftKes = new List<string>() { "111111", "222222", "333333", "444444" };
 4             var rightKes = new List<string>() { "333333", "444444", "555555", "666666" };
 5 
 6             Console.WriteLine("Left aggregate");
 7             Console.WriteLine(string.Join(',', leftKes));
 8             Console.WriteLine("-------------------------------------");
 9 
10             Console.WriteLine("Right aggregate");
11             Console.WriteLine(string.Join(',', rightKes));
12             Console.WriteLine("-------------------------------------");
13 
14             Console.WriteLine("Left Many ");
15             Console.WriteLine(string.Join(',', leftKes.Except(rightKes)));
16             Console.WriteLine("-------------------------------------");
17 
18             Console.WriteLine("Right Many ");
19             Console.WriteLine(string.Join(',', rightKes.Except(leftKes).ToList()));
20             Console.WriteLine("-------------------------------------");
21 
22             Console.WriteLine("Left and Right intersection ");
23             Console.WriteLine(string.Join(',', rightKes.Intersect(leftKes)));
24             Console.WriteLine("-------------------------------------");
25 
26             Console.WriteLine("Left and Right Union ");
27             Console.WriteLine(string.Join(',', leftKes.Union(rightKes)));
28             Console.WriteLine("-------------------------------------");
29 
30             Console.WriteLine("Left and Right Symmetric difference set ");
31             Console.WriteLine(string.Join(',', leftKes.Union(rightKes).Except(rightKes.Intersect(leftKes))));
32             Console.WriteLine("-------------------------------------");
33 
34         }

 

Running speed test code:

GitHub Code Library( https://github.com/axzxs2001/Asp.NetCoreExperiment/tree/master/Asp.NetCoreExperiment/Common/CSharp%E8%8E%B7%E5%8F%96%E4%B8%A4%E4%B8%AA%E9%9B%86%E5%90%88%E4%B8%AD%E7%9B%B8%E5%90%8C%E7%9A%84%E5%92%8C%E4%B8%8D%E5%90%8C%E7%9A%84%E7%BB%93%E6%9E%9C)

 1         static void Test()
 2         {
 3             Console.WriteLine("-------------------Start initializing the collection-------------------");
 4             #region Manufacturing data
 5             var watch = new Stopwatch();
 6             watch.Start();
 7             var listA = new List<string>();
 8             var listB = new List<string>();
 9             for (int i = 0; i < 1000000; i++)
10             {
11                 var id = Guid.NewGuid().ToString();
12                 listA.Add(id);
13             }
14             listB.AddRange(listA);
15             //Manufacture B Difference data
16             for (int i = 0; i < 10; i++)
17             {
18                 var tick = DateTime.Now.Ticks;
19                 var random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
20                 var index = random.Next(1, 100000);
21                 listB[index] = Guid.NewGuid().ToString();
22             }
23             watch.Stop();
24             TimeSpan span = watch.Elapsed;
25             Console.WriteLine("Total cost of manufacturing data{0}ms.", span.TotalMilliseconds);
26             #endregion
27 
28             #region compare
29             Console.WriteLine("-------------------Begin to compare-------------------");
30             Console.WriteLine("-------------------listA Outside the Central Intersection---------------------");
31             watch = new Stopwatch();
32             watch.Start();
33             var listD = listA.Except(listA.Intersect(listB));
34             watch.Stop();
35             span = watch.Elapsed;
36             Console.WriteLine("listA Total Expenses Outside the Central Intersection{0}ms.", span.TotalMilliseconds);
37             foreach (var item in listD)
38             {
39                 Console.WriteLine(item);
40             }
41             Console.WriteLine("----------------------listB Outside the Central Intersection--------------------");
42             watch = new Stopwatch();
43             watch.Start();
44             var listE = listB.Except(listA.Intersect(listB));
45             watch.Stop();
46             span = watch.Elapsed;
47             Console.WriteLine("listB Total Expenses Outside the Central Intersection{0}ms.", span.TotalMilliseconds);
48             foreach (var item in listE)
49             {
50                 Console.WriteLine(item);
51             }
52             Console.WriteLine("--------------listA and listB Outside the Central Intersection-------------------");
53             watch = new Stopwatch();
54             watch.Start();
55             var listF = listA.Concat(listB).Except(listA.Intersect(listB));
56             watch.Stop();
57             span = watch.Elapsed;
58             Console.WriteLine("listA and listB Total Expenses Outside the Central Intersection{0}ms.", span.TotalMilliseconds);
59             foreach (var item in listF)
60             {
61                 Console.WriteLine(item);
62             }
63             #endregion
64         }

Comparing the results of long running time:

My configuration is:

Tags: C# github

Posted on Tue, 08 Oct 2019 10:00:36 -0700 by MarineX69