A DESCRIPTION OF THE REQUEST : The java.lang.String#hashCode() method uses multiplication on 31 (h*31 + ... ) in hash code generation. More faster method is (h<<5) - h. Because x*31 == (x<<5) - x; (x<<5 == x*32); JUSTIFICATION : Need more faster hashCode method. EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - More faster hashCode generation. ACTUAL - Actual java.lang.String#hashCode() method uses multiplication operation in hash code generation. It more slow than shft and subtraction. ---------- BEGIN SOURCE ---------- public void testHashCode(){ int h1 = 28167, h2 = 28167; for (int i = 0; i < 1000; i++) { h1= h1*31; h2 = (h2<<5) - h2; assertEquals(h1,h2); } long c = 1000000000; long start = System.currentTimeMillis(); for (int i = 0; i < c; i++) { h1= h1*31; } long t1 = System.currentTimeMillis()-start; start = System.currentTimeMillis(); for (int i = 0; i < c; i++) { h2 = (h2<<5) - h2; } long t2 = System.currentTimeMillis() - start ; System.out.println("t1 - "+t1 + ", t2 - "+t2); System.out.println("dif-" +(t1-t2)); } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : Use (h<<5) - h instead h*32
|