Hm, hashCode7() has exactly the same program logic than hashCode1(), but by different syntax. hashCode8() should be worse than hashCode7(), because it loads the count variable twice.

If you compare the byte code by javah or jclasslib bytecode viewer (very nice tool), you will see that, am I wrong ?

-Ulf


Am 27.02.2010 18:50, schrieb Jason Mehrens:
Here are two more variants you might want to throw into the benchmark.

  public int hashCode6() {
        int h = hash;
        if (h == 0 && count > 0) {
            int off = offset;
            char val[] = value;
            int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
  }

  public int hashCode7() {
        int h = hash;
        int len;
        if (h == 0 && (len = count) > 0) {
            int off = offset;
            char val[] = value;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
  }

Jason


------------------------------------------------------------------------
Date: Sat, 27 Feb 2010 17:00:14 +0300
Subject: Re: Need reviewer for forward port of 6815768 (File.getXXXSpace) and 6815768 (String.hashCode)
From: dmitry.nadez...@gmail.com
To: core-libs-dev@openjdk.java.net

Ulf,

I ran Caliper benchmarks on different variants of hashCode().
http://code.google.com/p/caliper/
The results and the Caliper code are below:

  -Dima

--------- -d32 -clien
0% Scenario{vm=java -d32 -client, benchmark=HashCodeEmpty} 8,85ns; σ=0,06ns @ 3 trials 8% Scenario{vm=java -d32 -client, benchmark=HashCode1Empty} 7,20ns; σ=0,04ns @ 3 trials 17% Scenario{vm=java -d32 -client, benchmark=HashCode2Empty} 7,79ns; σ=0,32ns @ 10 trials 25% Scenario{vm=java -d32 -client, benchmark=HashCode3Empty} 7,88ns; σ=0,31ns @ 10 trials 33% Scenario{vm=java -d32 -client, benchmark=HashCode4Empty} 7,92ns; σ=0,31ns @ 10 trials 42% Scenario{vm=java -d32 -client, benchmark=HashCode5Empty} 7,06ns; σ=0,27ns @ 10 trials 50% Scenario{vm=java -d32 -client, benchmark=HashCodeLong} 145,22ns; σ=0,68ns @ 3 trials 58% Scenario{vm=java -d32 -client, benchmark=HashCode1Long} 144,58ns; σ=0,55ns @ 3 trials 67% Scenario{vm=java -d32 -client, benchmark=HashCode2Long} 167,55ns; σ=1,58ns @ 6 trials 75% Scenario{vm=java -d32 -client, benchmark=HashCode3Long} 122,87ns; σ=0,34ns @ 3 trials 83% Scenario{vm=java -d32 -client, benchmark=HashCode4Long} 122,69ns; σ=0,36ns @ 3 trials 92% Scenario{vm=java -d32 -client, benchmark=HashCode5Long} 126,11ns; σ=0,77ns @ 3 trials

     benchmark   ns logarithmic runtime
 HashCodeEmpty    9 X|||||||||||
HashCode1Empty    7 X||||||||||
HashCode2Empty    8 X|||||||||||
HashCode3Empty    8 X|||||||||||
HashCode4Empty    8 X|||||||||||
HashCode5Empty    7 X||||||||||
  HashCodeLong  145 XXXXXXXXXXXXXXXXXXXXXXXXXX|||
 HashCode1Long  145 XXXXXXXXXXXXXXXXXXXXXXXXX||||
 HashCode2Long  168 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 HashCode3Long  123 XXXXXXXXXXXXXXXXXXXXXX||||||
 HashCode4Long  123 XXXXXXXXXXXXXXXXXXXXX|||||||
 HashCode5Long  126 XXXXXXXXXXXXXXXXXXXXXX||||||

vm: java -d32 -client
--------
-------- -d32 -server
0% Scenario{vm=java -d32 -server, benchmark=HashCodeEmpty} 1,69ns; σ=0,01ns @ 3 trials 8% Scenario{vm=java -d32 -server, benchmark=HashCode1Empty} 2,00ns; σ=0,07ns @ 10 trials 17% Scenario{vm=java -d32 -server, benchmark=HashCode2Empty} 1,97ns; σ=0,01ns @ 3 trials 25% Scenario{vm=java -d32 -server, benchmark=HashCode3Empty} 1,55ns; σ=0,01ns @ 3 trials 33% Scenario{vm=java -d32 -server, benchmark=HashCode4Empty} 1,54ns; σ=0,01ns @ 3 trials 42% Scenario{vm=java -d32 -server, benchmark=HashCode5Empty} 2,32ns; σ=0,01ns @ 3 trials 50% Scenario{vm=java -d32 -server, benchmark=HashCodeLong} 113,01ns; σ=5,69ns @ 10 trials 58% Scenario{vm=java -d32 -server, benchmark=HashCode1Long} 101,49ns; σ=0,89ns @ 3 trials 67% Scenario{vm=java -d32 -server, benchmark=HashCode2Long} 102,93ns; σ=0,26ns @ 3 trials 75% Scenario{vm=java -d32 -server, benchmark=HashCode3Long} 100,29ns; σ=0,98ns @ 3 trials 83% Scenario{vm=java -d32 -server, benchmark=HashCode4Long} 100,00ns; σ=0,94ns @ 5 trials 92% Scenario{vm=java -d32 -server, benchmark=HashCode5Long} 112,29ns; σ=1,22ns @ 10 trials

     benchmark   ns logarithmic runtime
 HashCodeEmpty    2 |||
HashCode1Empty    2 ||||
HashCode2Empty    2 ||||
HashCode3Empty    2 ||
HashCode4Empty    2 ||
HashCode5Empty    2 |||||
  HashCodeLong  113 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 HashCode1Long  101 XXXXXXXXXXXXXXXXXXXXXXXXXX|||
 HashCode2Long  103 XXXXXXXXXXXXXXXXXXXXXXXXXXX||
 HashCode3Long  100 XXXXXXXXXXXXXXXXXXXXXXXXXX|||
 HashCode4Long  100 XXXXXXXXXXXXXXXXXXXXXXXXXX|||
 HashCode5Long  112 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

vm: java -d32 -server
------
------ -d64 -client
0% Scenario{vm=java -d64 -client, benchmark=HashCodeEmpty} 1,63ns; σ=0,01ns @ 3 trials 8% Scenario{vm=java -d64 -client, benchmark=HashCode1Empty} 1,41ns; σ=0,01ns @ 3 trials 17% Scenario{vm=java -d64 -client, benchmark=HashCode2Empty} 1,40ns; σ=0,01ns @ 3 trials 25% Scenario{vm=java -d64 -client, benchmark=HashCode3Empty} 1,42ns; σ=0,01ns @ 3 trials 33% Scenario{vm=java -d64 -client, benchmark=HashCode4Empty} 1,40ns; σ=0,00ns @ 3 trials 42% Scenario{vm=java -d64 -client, benchmark=HashCode5Empty} 4,29ns; σ=0,11ns @ 10 trials 50% Scenario{vm=java -d64 -client, benchmark=HashCodeLong} 101,74ns; σ=0,12ns @ 3 trials 58% Scenario{vm=java -d64 -client, benchmark=HashCode1Long} 102,66ns; σ=0,91ns @ 3 trials 67% Scenario{vm=java -d64 -client, benchmark=HashCode2Long} 100,41ns; σ=0,45ns @ 3 trials 75% Scenario{vm=java -d64 -client, benchmark=HashCode3Long} 97,41ns; σ=0,57ns @ 3 trials 83% Scenario{vm=java -d64 -client, benchmark=HashCode4Long} 97,92ns; σ=0,46ns @ 3 trials 92% Scenario{vm=java -d64 -client, benchmark=HashCode5Long} 107,27ns; σ=0,97ns @ 3 trials

     benchmark   ns logarithmic runtime
 HashCodeEmpty    2 |||
HashCode1Empty    1 ||
HashCode2Empty    1 ||
HashCode3Empty    1 ||
HashCode4Empty    1 ||
HashCode5Empty    4 X||||||||
  HashCodeLong  102 XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 HashCode1Long  103 XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 HashCode2Long  100 XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 HashCode3Long   97 XXXXXXXXXXXXXXXXXXXXXXXXXXX||
 HashCode4Long   98 XXXXXXXXXXXXXXXXXXXXXXXXXXX||
 HashCode5Long  107 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

vm: java -d64 -client
-------
------ -d64 -server
0% Scenario{vm=java -d64 -server, benchmark=HashCodeEmpty} 1,62ns; σ=0,00ns @ 3 trials 8% Scenario{vm=java -d64 -server, benchmark=HashCode1Empty} 1,42ns; σ=0,00ns @ 3 trials 17% Scenario{vm=java -d64 -server, benchmark=HashCode2Empty} 1,42ns; σ=0,01ns @ 3 trials 25% Scenario{vm=java -d64 -server, benchmark=HashCode3Empty} 1,42ns; σ=0,00ns @ 3 trials 33% Scenario{vm=java -d64 -server, benchmark=HashCode4Empty} 1,41ns; σ=0,01ns @ 3 trials 42% Scenario{vm=java -d64 -server, benchmark=HashCode5Empty} 4,20ns; σ=0,03ns @ 3 trials 50% Scenario{vm=java -d64 -server, benchmark=HashCodeLong} 102,05ns; σ=0,22ns @ 3 trials 58% Scenario{vm=java -d64 -server, benchmark=HashCode1Long} 102,33ns; σ=0,62ns @ 3 trials 67% Scenario{vm=java -d64 -server, benchmark=HashCode2Long} 100,70ns; σ=0,43ns @ 3 trials 75% Scenario{vm=java -d64 -server, benchmark=HashCode3Long} 96,92ns; σ=0,90ns @ 3 trials 83% Scenario{vm=java -d64 -server, benchmark=HashCode4Long} 98,22ns; σ=0,40ns @ 3 trials 92% Scenario{vm=java -d64 -server, benchmark=HashCode5Long} 108,22ns; σ=1,45ns @ 10 trials

     benchmark   ns logarithmic runtime
 HashCodeEmpty    2 |||
HashCode1Empty    1 ||
HashCode2Empty    1 ||
HashCode3Empty    1 ||
HashCode4Empty    1 ||
HashCode5Empty    4 X||||||||
  HashCodeLong  102 XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 HashCode1Long  102 XXXXXXXXXXXXXXXXXXXXXXXXXXXX|
 HashCode2Long  101 XXXXXXXXXXXXXXXXXXXXXXXXXXX||
 HashCode3Long   97 XXXXXXXXXXXXXXXXXXXXXXXXXX|||
 HashCode4Long   98 XXXXXXXXXXXXXXXXXXXXXXXXXXX||
 HashCode5Long  108 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

vm: java -d64 -server
-------------
------------- StringHashCodeBenchmark.java
package examples;

import com.google.caliper.SimpleBenchmark;
import java_lang.String;

public class StringHashCodeBenchmark extends SimpleBenchmark {

    private String EMPTY = new String("");
private String LONG = new String("@ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");

    public int timeHashCodeEmpty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode();
        }
        return dummy;
    }

    public int timeHashCode1Empty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode1();
        }
        return dummy;
    }

    public int timeHashCode2Empty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode2();
        }
        return dummy;
    }

    public int timeHashCode3Empty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode3();
        }
        return dummy;
    }

    public int timeHashCode4Empty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode4();
        }
        return dummy;
    }

    public int timeHashCode5Empty(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += EMPTY.hashCode5();
        }
        return dummy;
    }

    public int timeHashCodeLong(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode();
        }
        return dummy;
    }

    public int timeHashCode1Long(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode1();
        }
        return dummy;
    }

    public int timeHashCode2Long(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode2();
        }
        return dummy;
    }

    public int timeHashCode3Long(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode3();
        }
        return dummy;
    }

    public int timeHashCode4Long(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode4();
        }
        return dummy;
    }

    public int timeHashCode5Long(int reps) {
        int dummy = 0;
        for (int i = 0; i < reps; i++) {
            dummy += LONG.substring(1).hashCode5();
        }
        return dummy;
    }
}
----------------

On Sat, Feb 27, 2010 at 2:18 AM, Ulf Zibis <ulf.zi...@gmx.de <mailto:ulf.zi...@gmx.de>> wrote:

    Am 26.02.2010 20:32, schrieb Alan Bateman:

        Ulf Zibis wrote:
        For these other suggestions it would be great to create
        micro-benchmarks and try our your changes.

        For now though, as I said, I'm just fixing the method to avoid the
        store for the empty string case.


    Here the benchmark for my different approaches on non-empty strings:

    hashCode() warmup =     38,227 ms
    hashCode1() warmup =    323,462 ms
    hashCode() time =     12,066 ms
    hashCode1() time =     11,620 ms
    hashCodes sum = 0
    hashCode() warmup =     34,928 ms
    hashCode1() warmup =     19,010 ms
    hashCode2() warmup =     25,938 ms
    hashCode3() warmup =     22,428 ms
    hashCode4() warmup =     22,966 ms
    hashCode5() warmup =     21,372 ms
    hashCode() time =    418,962 ms
    hashCode1() time =    306,977 ms
    hashCode2() time =    296,899 ms
    hashCode3() time =    363,364 ms
    hashCode4() time =    286,098 ms
    hashCode5() time =    269,519 ms
    hashCodes sum = -1427881024

    See new revision:
    
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/src/java/lang/String.java?rev=955&view=markup
    
<https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/src/java/lang/String.java?rev=955&view=markup>

    -Ulf






------------------------------------------------------------------------
Hotmail: Powerful Free email with security by Microsoft. Get it now. <http://clk.atdmt.com/GBL/go/201469230/direct/01/>

Reply via email to