Get the App
SLTechnology News&Howtos  ›  Development  › 

How to compare HashSet between java and. Net

Shulou Source: shulou.com Published: 2022-06-02 20:22:52 10月02日 Update

This article is to share with you about how to compare java and. Net HashSet. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

Today, due to a work problem, we tested the execution of the code functionality agreed by C # and java and found a problem.

Compared with the HashSet.contains method, the performance under java is not as high as that of c #.

Private static final Logger log = Logger.getLogger (NewClass.class); public static void main (String [] args) {for (int j = 0; j)

< 5; j++) { HashSet ids = new HashSet(0); log.error("开始测试:" + j); int forCount = 200 * 10000; for (int i = 0; i < forCount; i++) { if (!ids.contains(i)) { ids.add(i); } } log.error("结束测试:" + j + " 执行次数:" + forCount); } } [04-12 16:16:57:427] ->

Start testing: 0

[04-12 16 / 16 / 58 / 063]-> end test: 0 execution times: 2000000

[04-12 16-14-16-58-064]-> start the test: 1

[04-12 16 / 16 / 58]-> finish the test: 1 execution times: 2000000

[04-12 16 140 16 14 14 58 8 8 5]-> start testing: 2

[04-12 16 / 16 / 58 / 993]-> end test: 2 execution times: 2000000

[04-12 16-14-16-58-94]-> start testing: 3

[04-12 16 16 59 140 24]-> finish the test: 3 times of execution: 2000000

[04-12 16 purl 16 purl 59 purl 249]-> start the test: 4

-> end test: 4 execution times: 2000000

It can be seen that the results of the java run performed 2 million retrieval inserts, and the execution time was more than 100ms, about.

Private static final Logger log = Logger.getLogger (NewClass.class); public static void main (String [] args) {for (int j = 0; j)

< 5; j++) { HashSet ids = new HashSet(0); log.error("开始测试:" + j); int forCount = 2000 * 10000; for (int i = 0; i < forCount; i++) { if (!ids.contains(i)) { ids.add(i); } } log.error("结束测试:" + j + " 执行次数:" + forCount); } } [04-12 16:18:09:345] ->

Start testing: 0

[04-12 16-18-24-24]-> finish the test: 0 times of execution: 20000000

[04-12 16-18-24-24-18]-> start testing: 1

[04-12 16-18-36-600]-> finish the test: 1 times of execution: 20000000

[04-12 16-18-36-600]-> start testing: 2

[04-12 16-18-44-31]-> finish the test: 2 execution times: 20000000

[04-12 16-18-44-31]-> start testing: 3

[04-12 16-18-51-1]-> finish the test: 3 times of execution: 20000000

[04-12 16-18-51-803]-> start testing: 4

-> end test: 4 times of execution: 20000000

The average execution time is about 9 seconds when the search insertion is completed in 2000.

Next, let's take a look at the running results of c#

Static void Main (string [] args) {for (int j = 0; j)

< 5; j++) { HashSet ids = new HashSet(); Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j); int forCount = 200 * 10000; for (int i = 0; i < forCount; i++) { if (!ids.Contains(i)) { ids.Add(i); } } Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount); } Console.ReadLine(); } 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万次的检索插入情况 static void Main(string[] args) { for (int j = 0; j < 5; j++) { HashSet ids = new HashSet(); Console.WriteLine(DateTime.Now.NowString() + "开始测试:" + j); int forCount = 2000 * 10000; for (int i = 0; i < forCount; i++) { if (!ids.Contains(i)) { ids.Add(i); } } Console.WriteLine(DateTime.Now.NowString() + "结束测试:" + j + " 执行次数:" + forCount); } Console.ReadLine(); } 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秒钟样子。 这个不晓得是不是量级的性能问题呢???? 接下来再看看,直接插入,。,因为插入也自带了检索条件的 private static final Logger log = Logger.getLogger(NewClass.class); public static void main(String[] args) { for (int j = 0; j < 5; j++) { HashSet ids = new HashSet(0); log.error("开始测试:" + j); int forCount = 2000 * 10000; for (int i = 0; i < forCount; i++) { ids.add(i); } log.error("结束测试:" + j + " 执行次数:" + forCount); } } [04-12 16:30:32:591] ->

Start testing: 0

[04-12 16-30-44-44]-> end test: 0 times of execution: 20000000

[04-12 16-30-44-44]-> start the test: 1

-> finish the test: 1 execution times: 20000000

[04-12 16-30-57-57]-> start testing: 2

[04-12 16-31-08-22]-> finish the test: 2 execution times: 20000000

[04-12 16-31-01-08-22]-> start testing: 3

[04-12 16-31-19-19]-> finish the test: 3 execution times: 20000000

[04-12 16-31-19-30]-> start testing: 4

[04-12 16-31-23-8]-> finish the test: 4 times of execution: 20000000

It takes more than 9 seconds to insert java alone and execute 20 million times.

Static void Main (string [] args) {for (int j = 0; j < 5; jacks +) {HashSet ids = new HashSet (); Console.WriteLine (DateTime.Now.NowString () + "start testing:" + j); int forCount = 2000 * 10000; for (int I = 0; I < forCount) DateTime.Now.NowString +) {ids.Add (I);} Console.WriteLine (DateTime.Now.NowString () + "end test:" + j + "execution times:" + forCount);} Console.ReadLine ();}

2015-04-12 16-12 32-32-35-55: start testing: 0

2015-04-12 16-12 32-36-36-064: end test: 0 execution times: 20000000

2015-04-12 16-12 32-36-36-065: start testing: 1

2015-04-12 16-12 32-36-36-879: end test: 1 execution times: 20000000

2015-04-12 16-12 32-36-36-79: start testing: 2

2015-04-12 16-12 32-32-37-15: finish the test: 2 times of execution: 20000000

2015-04-12 16-12 32-37-37-57: start testing: 3

2015-04-12 16-12 32-38-18: finish the test: 3 times of execution: 20000000

2015-04-12 16-12 32-38-67: start testing: 4

2015-04-12 16-12-32-32-39-19: finish the test: 4 times of execution: 20000000

C # 20 million inserts take less than 1 second to execute. Isn't it interesting?

I don't know if it's my computer problem, or my execution is wrong. Ladies and gentlemen, put forward some suggestions.

=

Here is the way to change it to string at the request of the viewing officer, and it also uses the way of guid generation to ensure that the code is exactly the same.

Static void Main (string [] args) {Console.ReadLine (); for (int j = 0; j < 5; jacks +) {serverID = j; id = 0; HashSet ids = new HashSet (); Console.WriteLine (DateTime.Now.NowString () + "start testing:" + j) Int forCount = 600 * 10000; for (int I = 0; I < forCount; iTunes +) {/ / long tempID = getId (); String tempID = Guid.NewGuid () .ToString (); bool add = ids.Add (tempID) If (! add) {Console.WriteLine (DateTime.Now.NowString () + "repeat:" + I + "" + tempID);}} Console.WriteLine (DateTime.Now.NowString () + "end test:" + j + "execution times:" + forCount) } Console.ReadLine ();}

2015-04-12 18-18-17-19-19-501: start testing: 0

2015-04-12 18-18-29-29: finish the test: 0 times of execution: 6000000

2015-04-12 18-18-17-29-29-7: start testing: 1

2015-04-12 18-18-19-17-39-15: finish the test: 1 execution: 6000000

2015-04-12 18-18-17-39-15: start testing: 2

2015-04-12 18-18-17-48-14-41: end of test: 2 execution times: 6000000

2015-04-12 18-18-17-48-14-41: start testing: 3

2015-04-12 18-18-17-56-5-5: finish testing: 3 times of execution: 6000000

2015-04-12 18 1756 256: start testing: 4

2015-04-12 18-18-04-14: finish the test: 4 times of execution: 6000000

The execution insert 600 completion time is about 9 seconds.

Private static final SimpleDateFormat DF2 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss:SSS:"); public static String getDateFormat1 () {return DF2.format (new Date ());} public static void main (String [] args) throws Exception {for (int j = 0; j < 5; Jake +) {serverID = j; id = 0; HashSet ids = new HashSet (0) System.out.println (getDateFormat1 () + "start testing:" + j); int forCount = 600 * 10000; for (int I = 0; I < forCount; iTunes +) {/ / long tempid = getId (); String tempid = UUID.randomUUID (). ToString (); boolean add = ids.add (tempid) If (! add) {System.out.println (getDateFormat1 () + "repeat:" + I + "" + tempid);}} System.out.println (getDateFormat1 () + "end test:" + j + "execution times:" + forCount);}}

2015-04-12 18-19-19-34-89: start testing: 0

2015-04-12 18-18-19-49-49-246: end of test: 0 execution times: 6000000

2015-04-12 18-18-19-49-49-24-4: start testing: 1

2015-04-12 18-18-12 20-00-12: finish the test: 1 execution times: 6000000

2015-04-12 18-18-20-00-5-16: start testing: 2

2015-04-12 18-12 18-20-10-10-10: end of test: 2 execution times: 6000000

2015-04-12 18-18-20-10-10-70: start testing: 3

2015-04-12 18-18-20-20-20-40: end of test: 3 execution times: 6000000

2015-04-12 18-18-20-20-20-40: start testing: 4

2015-04-12 18-18-20-31-12: end of test: 4 times of execution: 6000000

It's also 600. It's done in about 11 seconds.

The above is how to compare java with. Net HashSet. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Test number of times jacks time search problem situation next code performance way more watch officer knowledge article result censor work run wrong Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno NVidia Shulou Information Docker