欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

两个集合对比

程序员文章站 2022-06-29 11:30:22
在GitHub上有个项目,本来是作为自己研究学习.net core的Demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们。 项目名称:Asp.NetCoreExperiment 项目地址:https://github.c... ......

在github上有个项目,本来是作为自己研究学习.net core的demo,没想到很多同学在看,还给了很多星,所以觉得应该升成3.0,整理一下,写成博分享给学习.net core的同学们。

项目名称:asp.netcoreexperiment

项目地址:https://github.com/axzxs2001/asp.netcoreexperiment

 

今天先分享几个对集合知识点。

两个集合对比代码:

github代码库(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集合");
 7             console.writeline(string.join(',', leftkes));
 8             console.writeline("-------------------------------------");
 9 
10             console.writeline("right集合");
11             console.writeline(string.join(',', rightkes));
12             console.writeline("-------------------------------------");
13 
14             console.writeline("left多的 ");
15             console.writeline(string.join(',', leftkes.except(rightkes)));
16             console.writeline("-------------------------------------");
17 
18             console.writeline("right多的 ");
19             console.writeline(string.join(',', rightkes.except(leftkes).tolist()));
20             console.writeline("-------------------------------------");
21 
22             console.writeline("left和right交集 ");
23             console.writeline(string.join(',', rightkes.intersect(leftkes)));
24             console.writeline("-------------------------------------");
25 
26             console.writeline("left和right并集 ");
27             console.writeline(string.join(',', leftkes.union(rightkes)));
28             console.writeline("-------------------------------------");
29 
30             console.writeline("left和right对称差集 ");
31             console.writeline(string.join(',', leftkes.union(rightkes).except(rightkes.intersect(leftkes))));
32             console.writeline("-------------------------------------");
33 
34         }

 

运行速度测试代码:

github代码库(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("-------------------开始初始化集合-------------------");
 4             #region 制造数据
 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             //制造b的差异数据
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("制造数据总共花费{0}ms.", span.totalmilliseconds);
26             #endregion
27 
28             #region 比较
29             console.writeline("-------------------开始比较-------------------");
30             console.writeline("-------------------lista中交集外---------------------");
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中交集外 总共花费{0}ms.", span.totalmilliseconds);
37             foreach (var item in listd)
38             {
39                 console.writeline(item);
40             }
41             console.writeline("----------------------listb中交集外--------------------");
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中交集外 总共花费{0}ms.", span.totalmilliseconds);
48             foreach (var item in liste)
49             {
50                 console.writeline(item);
51             }
52             console.writeline("--------------lista和listb中交集外-------------------");
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和listb中交集外 总共花费{0}ms.", span.totalmilliseconds);
59             foreach (var item in listf)
60             {
61                 console.writeline(item);
62             }
63             #endregion
64         }

对比的运行时长结果:

两个集合对比

我的配置是:

两个集合对比