研究 研究而已 java和.net的HashSet对比

各位看官,,我不是在引发战争,,我只是想知道事情的真想,我源之于,一段代码我需要实现C#和java的两个版本功能,才发现这一个对比。讨论问题的实质,为什么会出现这样的问题。java和C#都很优秀。请大家不要偏激了。

今天,因为工作问题,测试了一下C#和java同意的代码功能执行情况,发现一个问题。

HashSet.contains 方法对比,在java下面性能居然没有c#的高。

 1     private static final Logger log = Logger.getLogger(NewClass.class);
 2 
 3     public static void main(String[] args) {
 4         for (int j = 0; j < 5; j++) {
 5             HashSet<Integer> ids = new HashSet<>(0);
 6             log.error("开始测试:" + j);
 7             int forCount = 200 * 10000;
 8             for (int i = 0; i < forCount; i++) {
 9                 if (!ids.contains(i)) {
10                     ids.add(i);
11                 }
12             }
13             log.error("结束测试:" + j + " 执行次数:" + forCount);
14         }
15     }

[04-12 16:16:57:427] -> 开始测试:0
[04-12 16:16:58:063] -> 结束测试:0 执行次数:2000000
[04-12 16:16:58:064] -> 开始测试:1
[04-12 16:16:58:835] -> 结束测试:1 执行次数:2000000
[04-12 16:16:58:835] -> 开始测试:2
[04-12 16:16:58:993] -> 结束测试:2 执行次数:2000000
[04-12 16:16:58:994] -> 开始测试:3
[04-12 16:16:59:247] -> 结束测试:3 执行次数:2000000
[04-12 16:16:59:249] -> 开始测试:4
[04-12 16:16:59:382] -> 结束测试:4 执行次数:2000000

看出java运行结果 执行200万次检索插入,执行时间100多毫秒,大约是。

 1     private static final Logger log = Logger.getLogger(NewClass.class);
 2 
 3     public static void main(String[] args) {
 4         for (int j = 0; j < 5; j++) {
 5             HashSet<Integer> ids = new HashSet<>(0);
 6             log.error("开始测试:" + j);
 7             int forCount = 2000 * 10000;
 8             for (int i = 0; i < forCount; i++) {
 9                 if (!ids.contains(i)) {
10                     ids.add(i);
11                 }
12             }
13             log.error("结束测试:" + j + " 执行次数:" + forCount);
14         }
15     }

[04-12 16:18:09:345] -> 开始测试:0
[04-12 16:18:24:835] -> 结束测试:0 执行次数:20000000
[04-12 16:18:24:836] -> 开始测试:1
[04-12 16:18:36:600] -> 结束测试:1 执行次数:20000000
[04-12 16:18:36:600] -> 开始测试:2
[04-12 16:18:44:331] -> 结束测试:2 执行次数:20000000
[04-12 16:18:44:331] -> 开始测试:3
[04-12 16:18:51:801] -> 结束测试:3 执行次数:20000000
[04-12 16:18:51:803] -> 开始测试:4
[04-12 16:19:01:277] -> 结束测试:4 执行次数:20000000

而执行2000完成检索插入的时候执行平均时间 大约是9秒左右

接下来我们看看c#的运行结果

 1     static void Main(string[] args)
 2         {
 3             for (int j = 0; j < 5; j++)
 4             {
 5                 HashSet<int> ids = new HashSet<int>();
 6                 Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j);
 7                 int forCount = 200 * 10000;
 8                 for (int i = 0; i < forCount; i++)
 9                 {
10                     if (!ids.Contains(i))
11                     {
12                         ids.Add(i);
13                     }
14                 }
15                 Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount);
16             }
17             Console.ReadLine();
18         }

2015-04-12 16:20:06:223:开始测试:0
2015-04-12 16:20:06:321:结束测试:0 执行次数:2000000
2015-04-12 16:20:06:322:开始测试:1
2015-04-12 16:20:06:413:结束测试:1 执行次数:2000000
2015-04-12 16:20:06:414:开始测试:2
2015-04-12 16:20:06:500:结束测试:2 执行次数:2000000
2015-04-12 16:20:06:500:开始测试:3
2015-04-12 16:20:06:616:结束测试:3 执行次数:2000000
2015-04-12 16:20:06:617:开始测试:4
2015-04-12 16:20:06:717:结束测试:4 执行次数:2000000

执行200万次检索插入,执行平均时间100毫秒左右,比java略胜一凑 

再看看2000万次的检索插入情况

 1         static void Main(string[] args)
 2         {
 3             for (int j = 0; j < 5; j++)
 4             {
 5                 HashSet<int> ids = new HashSet<int>();
 6                 Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j);
 7                 int forCount = 2000 * 10000;
 8                 for (int i = 0; i < forCount; i++)
 9                 {
10                     if (!ids.Contains(i))
11                     {
12                         ids.Add(i);
13                     }
14                 }
15                 Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount);
16             }
17             Console.ReadLine();
18         }

2015-04-12 16:20:51:746:开始测试:0
2015-04-12 16:20:52:633:结束测试:0 执行次数:20000000
2015-04-12 16:20:52:634:开始测试:1
2015-04-12 16:20:53:645:结束测试:1 执行次数:20000000
2015-04-12 16:20:53:645:开始测试:2
2015-04-12 16:20:54:615:结束测试:2 执行次数:20000000
2015-04-12 16:20:54:615:开始测试:3
2015-04-12 16:20:55:623:结束测试:3 执行次数:20000000
2015-04-12 16:20:55:624:开始测试:4
2015-04-12 16:20:56:561:结束测试:4 执行次数:20000000

看看2000万次的检索插入时间大约是1秒钟样子。

这个不晓得是不是量级的性能问题呢????

接下来再看看,直接插入,。,因为插入也自带了检索条件的

 1    private static final Logger log = Logger.getLogger(NewClass.class);
 2 
 3     public static void main(String[] args) {
 4         for (int j = 0; j < 5; j++) {
 5             HashSet<Integer> ids = new HashSet<>(0);
 6             log.error("开始测试:" + j);
 7             int forCount = 2000 * 10000;
 8             for (int i = 0; i < forCount; i++) {
 9                 ids.add(i);
10             }
11             log.error("结束测试:" + j + " 执行次数:" + forCount);
12         }
13     }

[04-12 16:30:32:591] -> 开始测试:0
[04-12 16:30:44:725] -> 结束测试:0 执行次数:20000000
[04-12 16:30:44:726] -> 开始测试:1
[04-12 16:30:57:535] -> 结束测试:1 执行次数:20000000
[04-12 16:30:57:536] -> 开始测试:2
[04-12 16:31:08:237] -> 结束测试:2 执行次数:20000000
[04-12 16:31:08:237] -> 开始测试:3
[04-12 16:31:19:306] -> 结束测试:3 执行次数:20000000
[04-12 16:31:19:309] -> 开始测试:4
[04-12 16:31:23:810] -> 结束测试:4 执行次数:20000000

单纯插入java执行2000万次的时间大约是9秒多,

 1   static void Main(string[] args)
 2         {
 3             for (int j = 0; j < 5; j++)
 4             {
 5                 HashSet<int> ids = new HashSet<int>();
 6                 Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j);
 7                 int forCount = 2000 * 10000;
 8                 for (int i = 0; i < forCount; i++)
 9                 {
10                         ids.Add(i);
11                 }
12                 Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount);
13             }
14             Console.ReadLine();
15         }

2015-04-12 16:32:35:355:开始测试:0
2015-04-12 16:32:36:064:结束测试:0 执行次数:20000000
2015-04-12 16:32:36:065:开始测试:1
2015-04-12 16:32:36:879:结束测试:1 执行次数:20000000
2015-04-12 16:32:36:879:开始测试:2
2015-04-12 16:32:37:657:结束测试:2 执行次数:20000000
2015-04-12 16:32:37:657:开始测试:3
2015-04-12 16:32:38:466:结束测试:3 执行次数:20000000
2015-04-12 16:32:38:467:开始测试:4
2015-04-12 16:32:39:238:结束测试:4 执行次数:20000000

c# 2000万次插入执行时间大约是1秒不到。,,是不是很有趣。。。

不知道是不是我电脑问题,还是我的执行情况不对。各位看官提出建议看看。。。

======================================================================

这里是应各位看官要求,改成string的方式,同样是用guid的生成方式,保证完全代码一模一样

 1  static void Main(string[] args)
 2         {
 3             Console.ReadLine();
 4             for (int j = 0; j < 5; j++)
 5             {
 6                 serverID = j;
 7                 id = 0;
 8                 HashSet<String> ids = new HashSet<String>();
 9                 Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j);
10                 int forCount = 600 * 10000;
11                 for (int i = 0; i < forCount; i++)
12                 {
13                     //long tempID = getId();
14                     String tempID = Guid.NewGuid().ToString();
15                     bool add = ids.Add(tempID);
16                     if (!add)
17                     {
18                         Console.WriteLine(DateTime.Now.NowString() + "重复:" + i + "  " + tempID);
19                     }
20                 }
21                 Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount);
22             }
23             Console.ReadLine();
24         }

2015-04-12 18:17:19:501:开始测试:0
2015-04-12 18:17:29:757:结束测试:0 执行次数:6000000
2015-04-12 18:17:29:757:开始测试:1
2015-04-12 18:17:39:582:结束测试:1 执行次数:6000000
2015-04-12 18:17:39:583:开始测试:2
2015-04-12 18:17:48:141:结束测试:2 执行次数:6000000
2015-04-12 18:17:48:141:开始测试:3
2015-04-12 18:17:56:255:结束测试:3 执行次数:6000000
2015-04-12 18:17:56:256:开始测试:4
2015-04-12 18:18:04:374:结束测试:4 执行次数:6000000

执行插入600完成执行时间大约是9秒

 1  private static final SimpleDateFormat DF2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS: ");
 2 
 3     public static String getDateFormat1() {
 4         return DF2.format(new Date());
 5     }
 6 
 7     public static void main(String[] args) throws Exception {
 8         for (int j = 0; j < 5; j++) {
 9             serverID = j;
10             id = 0;
11             HashSet<String> ids = new HashSet<>(0);
12             System.out.println(getDateFormat1() + "开始测试:" + j);
13             int forCount = 600 * 10000;
14             for (int i = 0; i < forCount; i++) {
15                 //long tempid = getId();
16                 String tempid = UUID.randomUUID().toString();
17                 boolean add = ids.add(tempid);
18                 if (!add) {
19                     System.out.println(getDateFormat1() + "重复: " + i + "    " + tempid);
20                 }
21             }
22             System.out.println(getDateFormat1() + " 结束测试:" + j + " 执行次数:" + forCount);
23         }
24     }

2015-04-12 18:19:34:589: 开始测试:0
2015-04-12 18:19:49:246: 结束测试:0 执行次数:6000000
2015-04-12 18:19:49:246: 开始测试:1
2015-04-12 18:20:00:516: 结束测试:1 执行次数:6000000
2015-04-12 18:20:00:516: 开始测试:2
2015-04-12 18:20:10:670: 结束测试:2 执行次数:6000000
2015-04-12 18:20:10:670: 开始测试:3
2015-04-12 18:20:20:401: 结束测试:3 执行次数:6000000
2015-04-12 18:20:20:401: 开始测试:4
2015-04-12 18:20:31:124: 结束测试:4 执行次数:6000000

同样是600完成大约是11秒

跪求保留标示符
/**
 * @author: Troy.Chen(失足程序员, 15388152619)
 * @version: 2021-07-20 10:55
 **/

C#版本代码 vs2010及以上工具可以

java 开发工具是netbeans 和 idea 版本,只有项目导入如果出现异常,请根据自己的工具调整


提供免费仓储。
最新的代码地址:↓↓↓
https://gitee.com/wuxindao

觉得我还可以,打赏一下吧,你的肯定是我努力的最大动力
    
原文地址:https://www.cnblogs.com/shizuchengxuyuan/p/4419777.html