Research and research. Comparison of Java and. NET HashSet

Source: Internet
Author: User

Today, you should test the implementation of C # and Java's agreed code functionality for work problems and find a problem.

Compared to the Hashset.contains method, the performance in Java is not as high as C #.

1     Private Static FinalLogger log = Logger.getlogger (Newclass.class);2 3      Public Static voidMain (string[] args) {4          for(intj = 0; J < 5; J + +) {5hashset<integer> ids =NewHashset<> (0);6Log.error ("Start test:" +j);7             intForcount = 200 * 10000;8              for(inti = 0; i < Forcount; i++) {9                 if(!ids.contains (i)) {Ten Ids.add (i); One                 } A             } -Log.error ("End test:" + j + "execution count:" +forcount); -         } the}

[04-12 16:16:57:427], start test: 0
[04-12 16:16:58:063], end test: 0 execution times: 2000000
[04-12 16:16:58:064], start test: 1
[04-12 16:16:58:835], end test: 1 execution times: 2000000
[04-12 16:16:58:835], start test: 2
[04-12 16:16:58:993], end test: 2 execution times: 2000000
[04-12 16:16:58:994], start test: 3
[04-12 16:16:59:247], end test: 3 execution times: 2000000
[04-12 16:16:59:249], start test: 4
[04-12 16:16:59:382], end test: 4 execution times: 2000000

See Java Run results performed 2 million retrieval inserts, execution time of more than 100 milliseconds, approximately.

1     Private Static FinalLogger log = Logger.getlogger (Newclass.class);2 3      Public Static voidMain (string[] args) {4          for(intj = 0; J < 5; J + +) {5hashset<integer> ids =NewHashset<> (0);6Log.error ("Start test:" +j);7             intForcount = 2000 * 10000;8              for(inti = 0; i < Forcount; i++) {9                 if(!ids.contains (i)) {Ten Ids.add (i); One                 } A             } -Log.error ("End test:" + j + "execution count:" +forcount); -         } the}

[04-12 16:18:09:345], start test: 0
[04-12 16:18:24:835], end test: 0 execution times: 20000000
[04-12 16:18:24:836], start test: 1
[04-12 16:18:36:600], end test: 1 execution times: 20000000
[04-12 16:18:36:600], start test: 2
[04-12 16:18:44:331], end test: 2 execution times: 20000000
[04-12 16:18:44:331], start test: 3
[04-12 16:18:51:801], end test: 3 execution times: 20000000
[04-12 16:18:51:803], start test: 4
[04-12 16:19:01:277], end test: 4 execution times: 20000000

While performing 2000 to complete the retrieval, the average execution time is about 9 seconds.

Now let's look at the results of C # operation

1     Static voidMain (string[] args)2         {3              for(intj =0; J <5; J + +)4             {5hashset<int> ids =Newhashset<int>();6Console.WriteLine (DateTime.Now.NowString () +"To start the test:"+j);7                 intForcount = $*10000;8                  for(inti =0; i < Forcount; i++)9                 {Ten                     if(!IDs. Contains (i)) One                     { A IDs. ADD (i); -                     } -                 } theConsole.WriteLine (DateTime.Now.NowString () +"End test:"+ j +"number of executions:"+forcount); -             } - console.readline (); -}

2015-04-12 16:20:06:223: Start test: 0
2015-04-12 16:20:06:321: End test: 0 execution times: 2000000
2015-04-12 16:20:06:322: Start test: 1
2015-04-12 16:20:06:413: End test: 1 execution times: 2000000
2015-04-12 16:20:06:414: Start test: 2
2015-04-12 16:20:06:500: End test: 2 execution times: 2000000
2015-04-12 16:20:06:500: Start test: 3
2015-04-12 16:20:06:616: End test: 3 execution times: 2000000
2015-04-12 16:20:06:617: Start test: 4
2015-04-12 16:20:06:717: End test: 4 execution times: 2000000

Perform 2 million retrieval inserts, execution average Time 100 milliseconds, slightly better than Java

And look at the 20 million-time search insert.

1         Static voidMain (string[] args)2         {3              for(intj =0; J <5; J + +)4             {5hashset<int> ids =Newhashset<int>();6Console.WriteLine (DateTime.Now.NowString () +"To start the test:"+j);7                 intForcount = -*10000;8                  for(inti =0; i < Forcount; i++)9                 {Ten                     if(!IDs. Contains (i)) One                     { A IDs. ADD (i); -                     } -                 } theConsole.WriteLine (DateTime.Now.NowString () +"End test:"+ j +"number of executions:"+forcount); -             } - console.readline (); -}

2015-04-12 16:20:51:746: Start test: 0
2015-04-12 16:20:52:633: End test: 0 execution times: 20000000
2015-04-12 16:20:52:634: Start test: 1
2015-04-12 16:20:53:645: End test: 1 execution times: 20000000
2015-04-12 16:20:53:645: Start test: 2
2015-04-12 16:20:54:615: End test: 2 execution times: 20000000
2015-04-12 16:20:54:615: Start test: 3
2015-04-12 16:20:55:623: End test: 3 execution times: 20000000
2015-04-12 16:20:55:624: Start test: 4
2015-04-12 16:20:56:561: End test: 4 execution times: 20000000

Look at the 20 million times the insertion time is approximately 1 seconds.

This does not know is the magnitude of the performance problem????

Let's take a look at it and insert it directly. , because the insert also comes with the search criteria

1    Private Static FinalLogger log = Logger.getlogger (Newclass.class);2 3      Public Static voidMain (string[] args) {4          for(intj = 0; J < 5; J + +) {5hashset<integer> ids =NewHashset<> (0);6Log.error ("Start test:" +j);7             intForcount = 2000 * 10000;8              for(inti = 0; i < Forcount; i++) {9 Ids.add (i);Ten             } OneLog.error ("End test:" + j + "execution count:" +forcount); A         } -}

[04-12 16:30:32:591], start test: 0
[04-12 16:30:44:725], end test: 0 execution times: 20000000
[04-12 16:30:44:726], start test: 1
[04-12 16:30:57:535], end test: 1 execution times: 20000000
[04-12 16:30:57:536], start test: 2
[04-12 16:31:08:237], end test: 2 execution times: 20000000
[04-12 16:31:08:237], start test: 3
[04-12 16:31:19:306], end test: 3 execution times: 20000000
[04-12 16:31:19:309], start test: 4
[04-12 16:31:23:810], end test: 4 execution times: 20000000

It takes about 9 seconds to insert Java 20 million times,

1   Static voidMain (string[] args)2         {3              for(intj =0; J <5; J + +)4             {5hashset<int> ids =Newhashset<int>();6Console.WriteLine (DateTime.Now.NowString () +"To start the test:"+j);7                 intForcount = -*10000;8                  for(inti =0; i < Forcount; i++)9                 {Ten IDs. ADD (i); One                 } AConsole.WriteLine (DateTime.Now.NowString () +"End test:"+ j +"number of executions:"+forcount); -             } - console.readline (); the}

2015-04-12 16:32:35:355: Start test: 0
2015-04-12 16:32:36:064: End test: 0 execution times: 20000000
2015-04-12 16:32:36:065: Start test: 1
2015-04-12 16:32:36:879: End test: 1 execution times: 20000000
2015-04-12 16:32:36:879: Start test: 2
2015-04-12 16:32:37:657: End test: 2 execution times: 20000000
2015-04-12 16:32:37:657: Start test: 3
2015-04-12 16:32:38:466: End test: 3 execution times: 20000000
2015-04-12 16:32:38:467: Start test: 4
2015-04-12 16:32:39:238: End test: 4 execution times: 20000000

C # 20 million times the insert execution time is less than 1 seconds. , is not very interesting ...

I do not know whether it is my computer problem, or I do not carry out the situation. Ladies and gentlemen, crossing make suggestions to see ...

Research and research. Comparison of Java and. NET HashSet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.