Research on java and. net HashSet comparison,. nethashset
You guys, I am not initiating a war. I just want to know what I really want. I am from a piece of code. I need to implement the functions of C # and java, this comparison is found. Discuss the essence of the problem and why it occurs. Both java and C # are excellent. Please do not be excited.
Today, due to work problems, I tested the execution of the code functions agreed by C # and java and found a problem.
Compared with the HashSet. contains method, the performance in java is not as high as that in 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 ("Start test:" + 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 ("End test:" + j + "executions:" + forCount); 14} 15}
[04-12 16: 16: 57: 427]-> Start test: 0
[04-12 16: 16: 58: 063]-> end test: 0 executions: 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
It can be seen that the java running result executes 2 million search inserts, And the execution time is more than 100 milliseconds, which is about.
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 ("Start test:" + 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 ("End test:" + j + "executions:" + forCount); 14} 15}
[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
The average execution time for 2000 of search inserts is about 9 seconds.
Next let's take a look at the running result of 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 () + "Start test:" + 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 () + "End test:" + j + "execution times:" + forCount); 16} 17 Console. readLine (); 18}
16: 20: 06: 223: Start test: 0
16: 20: 06: 321: End test: 0 execution times: 2000000
16: 20: 06: 322: Start test: 1
16: 20: 06: 413: End test: 1 execution times: 2000000
16: 20: 06: 414: Start test: 2
16: 20: 06: 500: End test: 2 execution times: 2000000
16: 20: 06: 500: Start test: 3
16: 20: 06: 616: End test: 3 times: 2000000
16: 20: 06: 617: Start test: 4
16: 20: 06: 717: End test: 4 times: 2000000
Execute 2 million search inserts, and the average execution time is about 100 milliseconds, slightly better than java
Let's look at the 20 million search Inserts
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 () + "Start test:" + 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 () + "End test:" + j + "execution times:" + forCount); 16} 17 Console. readLine (); 18}
16: 20: 51: 746: Start test: 0
16: 20: 52: 633: End test: 0 execution times: 20000000
16: 20: 52: 634: Start test: 1
16: 20: 53: 645: End test: 1 execution times: 20000000
16: 20: 53: 645: Start test: 2
16: 20: 54: 615: End test: 2 execution times: 20000000
2015-04-12 16: 20: 54: 615: Start test: 3
16: 20: 55: 623: End test: 3 execution times: 20000000
16: 20: 55: 624: Start test: 4
16: 20: 56: 561: End test: 4 execution times: 20000000
Check whether the insertion time of the first search is about 1 second.
I don't know if this is a performance problem of Magnitude ????
Next let's take a look and insert it directly ,., Because insert also has built-in search Condition
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 ("Start test:" + j); 7 int forCount = 2000*10000; 8 for (int I = 0; I <forCount; I ++) {9 ids. add (I); 10} 11 log. error ("End test:" + j + "executions:" + forCount); 12} 13}
[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 to execute 20 million times,
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 () + "Start test:" + 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 () + "End test:" + j + "execution times:" + forCount); 13} 14 Console. readLine (); 15}
16: 32: 35: 355: Start test: 0
16: 32: 36: 064: End test: 0 execution times: 20000000
2015-04-12 16: 32: 36: 065: Start test: 1
16: 32: 36: 879: End test: 1 execution times: 20000000
2015-04-12 16: 32: 36: 879: Start test: 2
16: 32: 37: 657: End test: 2 execution times: 20000000
16: 32: 37: 657: Start test: 3
16: 32: 38: 466: End test: 3 execution times: 20000000
16: 32: 38: 467: Start test: 4
16: 32: 39: 238: End test: 4 execution times: 20000000
C # the execution time of 20 million inserts is less than 1 second .,, Isn't it interesting...
I don't know if my computer is faulty or if my execution is wrong. Let's look at your suggestions...
========================================================== ====================================
Here we should change the form of string according to the requirements of the audience, and use the guid generation method to ensure that the Code is exactly the same.
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 () + "Start test:" + 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); 1 6 if (! Add) 17 {18 Console. writeLine (DateTime. now. nowString () + "repetition:" + I + "" + tempID); 19} 20} 21 Console. writeLine (DateTime. now. nowString () + "End test:" + j + "execution times:" + forCount); 22} 23 Console. readLine (); 24}
18: 17: 19: 501: Start test: 0
18: 17: 29: 757: End test: 0 execution times: 6000000
18: 17: 29: 757: Start test: 1
18: 17: 39: 582: End test: 1 execution times: 6000000
18: 17: 39: 583: Start test: 2
18: 17: 48: 141: End test: 2 execution times: 6000000
18: 17: 48: 141: Start test: 3
18: 17: 56: 255: End test: 3 execution times: 6000000
18: 17: 56: 256: Start test: 4
18: 18: 04: 374: End test: 4 execution times: 6000000
Execute insert 600. The execution time is about 9 seconds.
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 () + "Start test:" + j); 13 int f OrCount = 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 () + "repetition:" + I + "" + tempid); 20} 21} 22 System. out. println (getDateFormat1 () + "End test:" + j + "executions:" + forCount); 23} 24}
2015-04-12 18: 19: 34: 589: Start test: 0
18: 19: 49: 246: End test: 0 execution times: 6000000
2015-04-12 18: 19: 49: 246: Start test: 1
18: 20: 00: 516: End test: 1 execution times: 6000000
2015-04-12 18: 20: 00: 516: Start test: 2
18: 20: 10: 670: End test: 2 execution times: 6000000
18: 20: 10: 670: Start test: 3
18: 20: 20: 401: End test: 3 execution times: 6000000
2015-04-12 18: 20: 20: 401: Start test: 4
18: 20: 31: 124: End test: 4 execution times: 6000000
Similarly, 600 completed in about 11 seconds.