fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. HashMap<Integer,Integer> intHashMap = new HashMap<Integer,Integer>();
  13. HashMap<String,Integer> stringHashMap = new HashMap<String,Integer>();
  14.  
  15. int iterations = 1000000;
  16.  
  17. //int
  18. long ti = System.currentTimeMillis();
  19. for(int i=0;i<iterations;i++)
  20. {
  21. intHashMap.put(Integer.valueOf(i),i); ///Integer.valueOf() used to offset the speed implications of String.valueOf()
  22. }
  23. System.out.println("int put: " + (System.currentTimeMillis()-ti));
  24.  
  25. ti = System.currentTimeMillis();
  26. for(int i=0;i<iterations;i++)
  27. {
  28. intHashMap.get(Integer.valueOf(i));
  29. }
  30. System.out.println("int get: " + (System.currentTimeMillis()-ti));
  31.  
  32. //String
  33. ti = System.currentTimeMillis();
  34. for(int i=0;i<iterations;i++)
  35. {
  36. stringHashMap.put(String.valueOf(i),i);
  37. }
  38. System.out.println("string put: " + (System.currentTimeMillis()-ti));
  39.  
  40. ti = System.currentTimeMillis();
  41. for(int i=0;i<iterations;i++)
  42. {
  43. stringHashMap.get(String.valueOf(i));
  44. }
  45. System.out.println("string get: " + (System.currentTimeMillis()-ti));
  46.  
  47. intHashMap.clear();
  48. stringHashMap.clear();
  49. HashMap<String,Integer> stringHashMap2 = new HashMap<String,Integer>();
  50. HashMap<Integer,Integer> intHashMap2 = new HashMap<Integer,Integer>();
  51.  
  52.  
  53. //String
  54. ti = System.currentTimeMillis();
  55. for(int i=0;i<iterations;i++)
  56. {
  57. stringHashMap2.put(String.valueOf(i),i);
  58. }
  59. System.out.println("string put: " + (System.currentTimeMillis()-ti));
  60.  
  61. ti = System.currentTimeMillis();
  62. for(int i=0;i<iterations;i++)
  63. {
  64. stringHashMap2.get(String.valueOf(i));
  65. }
  66. System.out.println("string get: " + (System.currentTimeMillis()-ti));
  67.  
  68. //int
  69. ti = System.currentTimeMillis();
  70. for(int i=0;i<iterations;i++)
  71. {
  72. intHashMap2.put(Integer.valueOf(i),i);
  73. }
  74. System.out.println("int put: " + (System.currentTimeMillis()-ti));
  75.  
  76. ti = System.currentTimeMillis();
  77. for(int i=0;i<iterations;i++)
  78. {
  79. intHashMap2.get(Integer.valueOf(i));
  80. }
  81. System.out.println("int get: " + (System.currentTimeMillis()-ti));
  82.  
  83. }
  84. }
Success #stdin #stdout 4.54s 380352KB
stdin
Standard input is empty
stdout
int put:  566
int get:  411
string put:  1277
string get:  604
string put:  624
string get:  557
int put:  358
int get:  67