fork(2) download
  1. public class Main {
  2. public static void main(String[] args) {
  3. String s1 = null;
  4. String s2 = null;
  5.  
  6. // equal 1 (standard)
  7. s1 = "1234567890123456789012345678901234567890";
  8. s2 = "1234567890123456789012345678901234567890";
  9. long start = System.currentTimeMillis();
  10. for (long i = 0; i < 10000000000L; i += 1) {
  11. boolean b = s1.equals(s2);
  12. }
  13. System.out.println("equal: " + (System.currentTimeMillis() - start) + " milis");
  14.  
  15. // equal 2 (with length & hash check)
  16. s1 = "1234567890123456789012345678901234567891";
  17. s2 = "1234567890123456789012345678901234567891";
  18. start = System.currentTimeMillis();
  19. for (long i = 0; i < 10000000000L; i += 1) {
  20. boolean b = s1.length() == s2.length() && s1.hashCode() == s2.hashCode()
  21. && s1.equals(s2);
  22. }
  23. System.out.println("equal: " + (System.currentTimeMillis() - start) + " milis");
  24.  
  25. // equal 3 (with hash check)
  26. s1 = "1234567890123456789012345678901234567892";
  27. s2 = "1234567890123456789012345678901234567892";
  28. start = System.currentTimeMillis();
  29. for (long i = 0; i < 10000000000L; i += 1) {
  30. boolean b = s1.hashCode() == s2.hashCode() && s1.equals(s2);
  31. }
  32. System.out.println("equal: " + (System.currentTimeMillis() - start) + " milis");
  33.  
  34. // equal 4 (with length check)
  35. s1 = "1234567890123456789012345678901234567893";
  36. s2 = "1234567890123456789012345678901234567893";
  37. start = System.currentTimeMillis();
  38. for (long i = 0; i < 10000000000L; i += 1) {
  39. boolean b = s1.length() == s2.length() && s1.equals(s2);
  40. }
  41. System.out.println("equal: " + (System.currentTimeMillis() - start) + " milis");
  42. System.out.println();
  43.  
  44. // different 1 (standard)
  45. s1 = "1234567890123456789012345678901234567890";
  46. s2 = "1234567890123456789012345678901234567891";
  47. start = System.currentTimeMillis();
  48. for (long i = 0; i < 10000000000L; i += 1) {
  49. boolean b = s1.equals(s2);
  50. }
  51. System.out.println("different: " + (System.currentTimeMillis() - start) + " milis");
  52.  
  53. // different 2 (with length & hash check)
  54. s1 = "1234567890123456789012345678901234567891";
  55. s2 = "1234567890123456789012345678901234567892";
  56. start = System.currentTimeMillis();
  57. for (long i = 0; i < 10000000000L; i += 1) {
  58. boolean b = s1.length() == s2.length() && s1.hashCode() == s2.hashCode()
  59. && s1.equals(s2);
  60. }
  61. System.out.println("different: " + (System.currentTimeMillis() - start) + " milis");
  62.  
  63. // different 3 (with hash check)
  64. s1 = "1234567890123456789012345678901234567892";
  65. s2 = "1234567890123456789012345678901234567893";
  66. start = System.currentTimeMillis();
  67. for (long i = 0; i < 10000000000L; i += 1) {
  68. boolean b = s1.hashCode() == s2.hashCode() && s1.equals(s2);
  69. }
  70. System.out.println("different: " + (System.currentTimeMillis() - start) + " milis");
  71.  
  72. // different 4 (with length check)
  73. s1 = "1234567890123456789012345678901234567893";
  74. s2 = "1234567890123456789012345678901234567894";
  75. start = System.currentTimeMillis();
  76. for (long i = 0; i < 10000000000L; i += 1) {
  77. boolean b = s1.length() == s2.length() && s1.equals(s2);
  78. }
  79. System.out.println("different: " + (System.currentTimeMillis() - start) + " milis");
  80. System.out.println();
  81.  
  82. // different size 1 (standard)
  83. s1 = "1234567890123456789012345678901234567890";
  84. s2 = "12345678901234567890123456789012345678901";
  85. start = System.currentTimeMillis();
  86. for (long i = 0; i < 10000000000L; i += 1) {
  87. boolean b = s1.equals(s2);
  88. }
  89. System.out.println("different size: " + (System.currentTimeMillis() - start) + " milis");
  90.  
  91. // different size 2 (with length & hash check)
  92. s1 = "1234567890123456789012345678901234567891";
  93. s2 = "12345678901234567890123456789012345678912";
  94. start = System.currentTimeMillis();
  95. for (long i = 0; i < 10000000000L; i += 1) {
  96. boolean b = s1.length() == s2.length() && s1.hashCode() == s2.hashCode()
  97. && s1.equals(s2);
  98. }
  99. System.out.println("different size: " + (System.currentTimeMillis() - start) + " milis");
  100.  
  101. // different size 3 (with hash check)
  102. s1 = "1234567890123456789012345678901234567892";
  103. s2 = "12345678901234567890123456789012345678923";
  104. start = System.currentTimeMillis();
  105. for (long i = 0; i < 10000000000L; i += 1) {
  106. boolean b = s1.hashCode() == s2.hashCode() && s1.equals(s2);
  107. }
  108. System.out.println("different size: " + (System.currentTimeMillis() - start) + " milis");
  109.  
  110. // different size 4 (with length check)
  111. s1 = "1234567890123456789012345678901234567893";
  112. s2 = "12345678901234567890123456789012345678934";
  113. start = System.currentTimeMillis();
  114. for (long i = 0; i < 10000000000L; i += 1) {
  115. boolean b = s1.length() == s2.length() && s1.equals(s2);
  116. }
  117. System.out.println("different size: " + (System.currentTimeMillis() - start) + " milis");
  118. }
  119. }
  120.  
Time limit exceeded #stdin #stdout 5s 380736KB
stdin
Standard input is empty
stdout
Standard output is empty