Skip to content

Instantly share code, notes, and snippets.

@suzukitadashi
Last active October 11, 2016 06:32

Revisions

  1. suzukitadashi revised this gist Oct 11, 2016. 2 changed files with 29 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions TestEquals.java
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,6 @@
    package jmh18;

    import java.util.concurrent.TimeUnit;

    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.BenchmarkMode;
    import org.openjdk.jmh.annotations.Mode;
    import org.openjdk.jmh.annotations.OutputTimeUnit;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.RunnerException;
    import org.openjdk.jmh.runner.options.Options;
    @@ -20,6 +13,7 @@ public class TestEquals {
    public static void main(String[] args) throws RunnerException {
    System.out.println(value1.equalsFor(value2));
    System.out.println(value1.equalsWhile(value2));
    System.out.println(value1.equalsWhile2(value2));

    Options opt = new OptionsBuilder().include(TestEquals.class.getSimpleName()).warmupIterations(10)
    .measurementIterations(5).forks(2).build();
    @@ -50,6 +44,13 @@ public void testWhile() {

    }


    @Benchmark
    public void testWhile2() {

    for(int i = 0; i < 10000; i++) {
    boolean b = value1.equalsWhile2(value2);
    }

    }

    }
    20 changes: 20 additions & 0 deletions TestString.java
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,26 @@ public boolean equalsWhile(Object anObject) {
    }
    return false;
    }

    public boolean equalsWhile2(Object anObject) {
    if (this == anObject) {
    return true;
    }
    if (anObject instanceof TestString) {
    TestString anotherString = (TestString) anObject;
    int n = value.length;
    if (n == anotherString.value.length) {
    char v1[] = value;
    char v2[] = anotherString.value;
    while (n-- != 0) {
    if (v1[n] != v2[n])
    return false;
    }
    return true;
    }
    }
    return false;
    }

    public boolean equalsFor(Object anObject) {
    if (this == anObject) {
  2. suzukitadashi revised this gist Oct 11, 2016. 1 changed file with 171 additions and 0 deletions.
    171 changes: 171 additions & 0 deletions 結果2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,171 @@
    true
    true
    true
    # JMH 1.15 (released 11 days ago)
    # VM version: JDK 1.8.0_102, VM 25.102-b14
    # VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 10 iterations, 1 s each
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: jmh18.TestEquals.testFor

    # Run progress: 0.00% complete, ETA 00:01:30
    # Fork: 1 of 2
    # Warmup Iteration 1: 7547.443 ops/s
    # Warmup Iteration 2: 7581.998 ops/s
    # Warmup Iteration 3: 7810.046 ops/s
    # Warmup Iteration 4: 7808.333 ops/s
    # Warmup Iteration 5: 7668.112 ops/s
    # Warmup Iteration 6: 7786.944 ops/s
    # Warmup Iteration 7: 7800.719 ops/s
    # Warmup Iteration 8: 7807.167 ops/s
    # Warmup Iteration 9: 7809.493 ops/s
    # Warmup Iteration 10: 7801.846 ops/s
    Iteration 1: 7808.384 ops/s
    Iteration 2: 7812.139 ops/s
    Iteration 3: 7796.067 ops/s
    Iteration 4: 7814.271 ops/s
    Iteration 5: 7288.444 ops/s

    # Run progress: 16.67% complete, ETA 00:01:17
    # Fork: 2 of 2
    # Warmup Iteration 1: 7758.245 ops/s
    # Warmup Iteration 2: 7806.648 ops/s
    # Warmup Iteration 3: 7802.205 ops/s
    # Warmup Iteration 4: 7796.352 ops/s
    # Warmup Iteration 5: 7807.905 ops/s
    # Warmup Iteration 6: 7802.360 ops/s
    # Warmup Iteration 7: 7771.143 ops/s
    # Warmup Iteration 8: 7809.078 ops/s
    # Warmup Iteration 9: 7809.560 ops/s
    # Warmup Iteration 10: 7810.257 ops/s
    Iteration 1: 7809.623 ops/s
    Iteration 2: 7803.750 ops/s
    Iteration 3: 7818.244 ops/s
    Iteration 4: 7701.852 ops/s
    Iteration 5: 7267.833 ops/s


    Result "testFor":
    7692.061 ±(99.9%) 333.863 ops/s [Average]
    (min, avg, max) = (7267.833, 7692.061, 7818.244), stdev = 220.830
    CI (99.9%): [7358.198, 8025.924] (assumes normal distribution)


    # JMH 1.15 (released 11 days ago)
    # VM version: JDK 1.8.0_102, VM 25.102-b14
    # VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 10 iterations, 1 s each
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: jmh18.TestEquals.testWhile

    # Run progress: 33.33% complete, ETA 00:01:01
    # Fork: 1 of 2
    # Warmup Iteration 1: 6406.930 ops/s
    # Warmup Iteration 2: 6558.244 ops/s
    # Warmup Iteration 3: 6565.609 ops/s
    # Warmup Iteration 4: 6449.827 ops/s
    # Warmup Iteration 5: 6564.543 ops/s
    # Warmup Iteration 6: 6560.414 ops/s
    # Warmup Iteration 7: 6558.379 ops/s
    # Warmup Iteration 8: 6561.109 ops/s
    # Warmup Iteration 9: 6553.590 ops/s
    # Warmup Iteration 10: 6563.769 ops/s
    Iteration 1: 6552.003 ops/s
    Iteration 2: 6557.265 ops/s
    Iteration 3: 6552.025 ops/s
    Iteration 4: 6532.713 ops/s
    Iteration 5: 6561.580 ops/s

    # Run progress: 50.00% complete, ETA 00:00:46
    # Fork: 2 of 2
    # Warmup Iteration 1: 6485.661 ops/s
    # Warmup Iteration 2: 6552.764 ops/s
    # Warmup Iteration 3: 6555.554 ops/s
    # Warmup Iteration 4: 6464.064 ops/s
    # Warmup Iteration 5: 6557.037 ops/s
    # Warmup Iteration 6: 6555.314 ops/s
    # Warmup Iteration 7: 6559.864 ops/s
    # Warmup Iteration 8: 6557.996 ops/s
    # Warmup Iteration 9: 6559.901 ops/s
    # Warmup Iteration 10: 6558.208 ops/s
    Iteration 1: 6567.594 ops/s
    Iteration 2: 6561.070 ops/s
    Iteration 3: 6567.033 ops/s
    Iteration 4: 6558.485 ops/s
    Iteration 5: 6503.759 ops/s


    Result "testWhile":
    6551.353 ±(99.9%) 29.420 ops/s [Average]
    (min, avg, max) = (6503.759, 6551.353, 6567.594), stdev = 19.459
    CI (99.9%): [6521.933, 6580.772] (assumes normal distribution)


    # JMH 1.15 (released 11 days ago)
    # VM version: JDK 1.8.0_102, VM 25.102-b14
    # VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 10 iterations, 1 s each
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Throughput, ops/time
    # Benchmark: jmh18.TestEquals.testWhile2

    # Run progress: 66.67% complete, ETA 00:00:30
    # Fork: 1 of 2
    # Warmup Iteration 1: 7457.030 ops/s
    # Warmup Iteration 2: 7301.703 ops/s
    # Warmup Iteration 3: 7594.812 ops/s
    # Warmup Iteration 4: 7582.258 ops/s
    # Warmup Iteration 5: 7612.803 ops/s
    # Warmup Iteration 6: 7577.697 ops/s
    # Warmup Iteration 7: 7604.652 ops/s
    # Warmup Iteration 8: 7607.497 ops/s
    # Warmup Iteration 9: 7621.722 ops/s
    # Warmup Iteration 10: 7597.556 ops/s
    Iteration 1: 7606.510 ops/s
    Iteration 2: 7591.627 ops/s
    Iteration 3: 7548.231 ops/s
    Iteration 4: 7622.434 ops/s
    Iteration 5: 7326.982 ops/s

    # Run progress: 83.33% complete, ETA 00:00:15
    # Fork: 2 of 2
    # Warmup Iteration 1: 7564.975 ops/s
    # Warmup Iteration 2: 7619.918 ops/s
    # Warmup Iteration 3: 7630.326 ops/s
    # Warmup Iteration 4: 7624.609 ops/s
    # Warmup Iteration 5: 7619.486 ops/s
    # Warmup Iteration 6: 7616.613 ops/s
    # Warmup Iteration 7: 7628.601 ops/s
    # Warmup Iteration 8: 7625.874 ops/s
    # Warmup Iteration 9: 7636.233 ops/s
    # Warmup Iteration 10: 7627.183 ops/s
    Iteration 1: 7619.933 ops/s
    Iteration 2: 7626.321 ops/s
    Iteration 3: 7630.787 ops/s
    Iteration 4: 7622.516 ops/s
    Iteration 5: 7263.013 ops/s


    Result "testWhile2":
    7545.835 ±(99.9%) 204.459 ops/s [Average]
    (min, avg, max) = (7263.013, 7545.835, 7630.787), stdev = 135.237
    CI (99.9%): [7341.376, 7750.295] (assumes normal distribution)


    # Run complete. Total time: 00:01:32

    Benchmark Mode Cnt Score Error Units
    TestEquals.testFor thrpt 10 7692.061 ± 333.863 ops/s
    TestEquals.testWhile thrpt 10 6551.353 ± 29.420 ops/s
    TestEquals.testWhile2 thrpt 10 7545.835 ± 204.459 ops/s
  3. suzukitadashi revised this gist Oct 11, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions TestEquals.java
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,6 @@

    import obj.TestString;

    @State(Scope.Thread)
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public class TestEquals {

    public static void main(String[] args) throws RunnerException {
  4. suzukitadashi revised this gist Oct 11, 2016. 1 changed file with 74 additions and 74 deletions.
    148 changes: 74 additions & 74 deletions 結果.txt
    Original file line number Diff line number Diff line change
    @@ -8,50 +8,50 @@ true
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark mode: Throughput, ops/time
    # Benchmark: jmh18.TestEquals.testFor

    # Run progress: 0.00% complete, ETA 00:01:00
    # Fork: 1 of 2
    # Warmup Iteration 1: 129900.562 ns/op
    # Warmup Iteration 2: 131671.480 ns/op
    # Warmup Iteration 3: 133939.190 ns/op
    # Warmup Iteration 4: 132425.025 ns/op
    # Warmup Iteration 5: 156385.078 ns/op
    # Warmup Iteration 6: 150443.349 ns/op
    # Warmup Iteration 7: 132590.798 ns/op
    # Warmup Iteration 8: 131055.211 ns/op
    # Warmup Iteration 9: 128987.350 ns/op
    # Warmup Iteration 10: 129475.632 ns/op
    Iteration 1: 128953.568 ns/op
    Iteration 2: 130241.365 ns/op
    Iteration 3: 128743.183 ns/op
    Iteration 4: 128631.932 ns/op
    Iteration 5: 132243.733 ns/op
    # Warmup Iteration 1: 7581.564 ops/s
    # Warmup Iteration 2: 7766.917 ops/s
    # Warmup Iteration 3: 7782.609 ops/s
    # Warmup Iteration 4: 7778.253 ops/s
    # Warmup Iteration 5: 7679.957 ops/s
    # Warmup Iteration 6: 7695.138 ops/s
    # Warmup Iteration 7: 7719.328 ops/s
    # Warmup Iteration 8: 7742.130 ops/s
    # Warmup Iteration 9: 7764.089 ops/s
    # Warmup Iteration 10: 7770.489 ops/s
    Iteration 1: 7755.916 ops/s
    Iteration 2: 7726.641 ops/s
    Iteration 3: 7785.200 ops/s
    Iteration 4: 7694.890 ops/s
    Iteration 5: 7245.042 ops/s

    # Run progress: 25.00% complete, ETA 00:00:46
    # Run progress: 25.00% complete, ETA 00:00:47
    # Fork: 2 of 2
    # Warmup Iteration 1: 129274.214 ns/op
    # Warmup Iteration 2: 130875.347 ns/op
    # Warmup Iteration 3: 131635.726 ns/op
    # Warmup Iteration 4: 132477.601 ns/op
    # Warmup Iteration 5: 129186.761 ns/op
    # Warmup Iteration 6: 129110.621 ns/op
    # Warmup Iteration 7: 128450.350 ns/op
    # Warmup Iteration 8: 130720.949 ns/op
    # Warmup Iteration 9: 154381.269 ns/op
    # Warmup Iteration 10: 141566.283 ns/op
    Iteration 1: 138327.027 ns/op
    Iteration 2: 131209.843 ns/op
    Iteration 3: 137521.283 ns/op
    Iteration 4: 132556.907 ns/op
    Iteration 5: 131570.339 ns/op
    # Warmup Iteration 1: 7686.943 ops/s
    # Warmup Iteration 2: 7688.321 ops/s
    # Warmup Iteration 3: 7765.124 ops/s
    # Warmup Iteration 4: 7752.685 ops/s
    # Warmup Iteration 5: 7705.651 ops/s
    # Warmup Iteration 6: 7775.333 ops/s
    # Warmup Iteration 7: 7598.137 ops/s
    # Warmup Iteration 8: 7649.973 ops/s
    # Warmup Iteration 9: 7764.624 ops/s
    # Warmup Iteration 10: 7785.825 ops/s
    Iteration 1: 7777.973 ops/s
    Iteration 2: 7786.480 ops/s
    Iteration 3: 7701.670 ops/s
    Iteration 4: 7761.747 ops/s
    Iteration 5: 7147.334 ops/s


    Result "testFor":
    131999.918 ±(99.9%) 5188.964 ns/op [Average]
    (min, avg, max) = (128631.932, 131999.918, 138327.027), stdev = 3432.178
    CI (99.9%): [126810.954, 137188.882] (assumes normal distribution)
    7638.289 ±(99.9%) 357.359 ops/s [Average]
    (min, avg, max) = (7147.334, 7638.289, 7786.480), stdev = 236.371
    CI (99.9%): [7280.930, 7995.648] (assumes normal distribution)


    # JMH 1.15 (released 11 days ago)
    @@ -62,54 +62,54 @@ Result "testFor":
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark mode: Throughput, ops/time
    # Benchmark: jmh18.TestEquals.testWhile

    # Run progress: 50.00% complete, ETA 00:00:30
    # Run progress: 50.00% complete, ETA 00:00:31
    # Fork: 1 of 2
    # Warmup Iteration 1: 154492.802 ns/op
    # Warmup Iteration 2: 159377.851 ns/op
    # Warmup Iteration 3: 165431.894 ns/op
    # Warmup Iteration 4: 155956.235 ns/op
    # Warmup Iteration 5: 155576.361 ns/op
    # Warmup Iteration 6: 153299.612 ns/op
    # Warmup Iteration 7: 155696.111 ns/op
    # Warmup Iteration 8: 155724.180 ns/op
    # Warmup Iteration 9: 153210.150 ns/op
    # Warmup Iteration 10: 155278.559 ns/op
    Iteration 1: 158486.474 ns/op
    Iteration 2: 157945.836 ns/op
    Iteration 3: 159126.614 ns/op
    Iteration 4: 166451.356 ns/op
    Iteration 5: 160550.279 ns/op
    # Warmup Iteration 1: 6456.929 ops/s
    # Warmup Iteration 2: 6497.899 ops/s
    # Warmup Iteration 3: 6539.596 ops/s
    # Warmup Iteration 4: 6494.194 ops/s
    # Warmup Iteration 5: 6521.043 ops/s
    # Warmup Iteration 6: 6488.747 ops/s
    # Warmup Iteration 7: 6537.132 ops/s
    # Warmup Iteration 8: 6539.461 ops/s
    # Warmup Iteration 9: 6510.390 ops/s
    # Warmup Iteration 10: 6533.177 ops/s
    Iteration 1: 6428.383 ops/s
    Iteration 2: 6525.365 ops/s
    Iteration 3: 6533.350 ops/s
    Iteration 4: 6538.791 ops/s
    Iteration 5: 6400.482 ops/s

    # Run progress: 75.00% complete, ETA 00:00:15
    # Fork: 2 of 2
    # Warmup Iteration 1: 156200.249 ns/op
    # Warmup Iteration 2: 160897.541 ns/op
    # Warmup Iteration 3: 169560.297 ns/op
    # Warmup Iteration 4: 170340.118 ns/op
    # Warmup Iteration 5: 165995.662 ns/op
    # Warmup Iteration 6: 157818.608 ns/op
    # Warmup Iteration 7: 158936.884 ns/op
    # Warmup Iteration 8: 160276.063 ns/op
    # Warmup Iteration 9: 156367.417 ns/op
    # Warmup Iteration 10: 154037.557 ns/op
    Iteration 1: 153508.761 ns/op
    Iteration 2: 157928.712 ns/op
    Iteration 3: 153968.506 ns/op
    Iteration 4: 153312.665 ns/op
    Iteration 5: 158159.510 ns/op
    # Warmup Iteration 1: 6462.299 ops/s
    # Warmup Iteration 2: 6516.758 ops/s
    # Warmup Iteration 3: 6533.068 ops/s
    # Warmup Iteration 4: 6391.113 ops/s
    # Warmup Iteration 5: 6433.837 ops/s
    # Warmup Iteration 6: 6529.200 ops/s
    # Warmup Iteration 7: 6456.164 ops/s
    # Warmup Iteration 8: 6466.758 ops/s
    # Warmup Iteration 9: 4372.381 ops/s
    # Warmup Iteration 10: 6458.778 ops/s
    Iteration 1: 6518.393 ops/s
    Iteration 2: 6451.780 ops/s
    Iteration 3: 6538.547 ops/s
    Iteration 4: 6454.980 ops/s
    Iteration 5: 6505.108 ops/s


    Result "testWhile":
    157943.871 ±(99.9%) 5916.507 ns/op [Average]
    (min, avg, max) = (153312.665, 157943.871, 166451.356), stdev = 3913.403
    CI (99.9%): [152027.364, 163860.378] (assumes normal distribution)
    6489.518 ±(99.9%) 77.084 ops/s [Average]
    (min, avg, max) = (6400.482, 6489.518, 6538.791), stdev = 50.986
    CI (99.9%): [6412.433, 6566.602] (assumes normal distribution)


    # Run complete. Total time: 00:01:01
    # Run complete. Total time: 00:01:02

    Benchmark Mode Cnt Score Error Units
    TestEquals.testFor avgt 10 131999.918 ± 5188.964 ns/op
    TestEquals.testWhile avgt 10 157943.871 ± 5916.507 ns/op
    Benchmark Mode Cnt Score Error Units
    TestEquals.testFor thrpt 10 7638.289 ± 357.359 ops/s
    TestEquals.testWhile thrpt 10 6489.518 ± 77.084 ops/s
  5. suzukitadashi revised this gist Oct 11, 2016. 1 changed file with 115 additions and 0 deletions.
    115 changes: 115 additions & 0 deletions 結果.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,115 @@
    true
    true
    # JMH 1.15 (released 11 days ago)
    # VM version: JDK 1.8.0_102, VM 25.102-b14
    # VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 10 iterations, 1 s each
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: jmh18.TestEquals.testFor

    # Run progress: 0.00% complete, ETA 00:01:00
    # Fork: 1 of 2
    # Warmup Iteration 1: 129900.562 ns/op
    # Warmup Iteration 2: 131671.480 ns/op
    # Warmup Iteration 3: 133939.190 ns/op
    # Warmup Iteration 4: 132425.025 ns/op
    # Warmup Iteration 5: 156385.078 ns/op
    # Warmup Iteration 6: 150443.349 ns/op
    # Warmup Iteration 7: 132590.798 ns/op
    # Warmup Iteration 8: 131055.211 ns/op
    # Warmup Iteration 9: 128987.350 ns/op
    # Warmup Iteration 10: 129475.632 ns/op
    Iteration 1: 128953.568 ns/op
    Iteration 2: 130241.365 ns/op
    Iteration 3: 128743.183 ns/op
    Iteration 4: 128631.932 ns/op
    Iteration 5: 132243.733 ns/op

    # Run progress: 25.00% complete, ETA 00:00:46
    # Fork: 2 of 2
    # Warmup Iteration 1: 129274.214 ns/op
    # Warmup Iteration 2: 130875.347 ns/op
    # Warmup Iteration 3: 131635.726 ns/op
    # Warmup Iteration 4: 132477.601 ns/op
    # Warmup Iteration 5: 129186.761 ns/op
    # Warmup Iteration 6: 129110.621 ns/op
    # Warmup Iteration 7: 128450.350 ns/op
    # Warmup Iteration 8: 130720.949 ns/op
    # Warmup Iteration 9: 154381.269 ns/op
    # Warmup Iteration 10: 141566.283 ns/op
    Iteration 1: 138327.027 ns/op
    Iteration 2: 131209.843 ns/op
    Iteration 3: 137521.283 ns/op
    Iteration 4: 132556.907 ns/op
    Iteration 5: 131570.339 ns/op


    Result "testFor":
    131999.918 ±(99.9%) 5188.964 ns/op [Average]
    (min, avg, max) = (128631.932, 131999.918, 138327.027), stdev = 3432.178
    CI (99.9%): [126810.954, 137188.882] (assumes normal distribution)


    # JMH 1.15 (released 11 days ago)
    # VM version: JDK 1.8.0_102, VM 25.102-b14
    # VM invoker: C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.exe
    # VM options: -Dfile.encoding=UTF-8
    # Warmup: 10 iterations, 1 s each
    # Measurement: 5 iterations, 1 s each
    # Timeout: 10 min per iteration
    # Threads: 1 thread, will synchronize iterations
    # Benchmark mode: Average time, time/op
    # Benchmark: jmh18.TestEquals.testWhile

    # Run progress: 50.00% complete, ETA 00:00:30
    # Fork: 1 of 2
    # Warmup Iteration 1: 154492.802 ns/op
    # Warmup Iteration 2: 159377.851 ns/op
    # Warmup Iteration 3: 165431.894 ns/op
    # Warmup Iteration 4: 155956.235 ns/op
    # Warmup Iteration 5: 155576.361 ns/op
    # Warmup Iteration 6: 153299.612 ns/op
    # Warmup Iteration 7: 155696.111 ns/op
    # Warmup Iteration 8: 155724.180 ns/op
    # Warmup Iteration 9: 153210.150 ns/op
    # Warmup Iteration 10: 155278.559 ns/op
    Iteration 1: 158486.474 ns/op
    Iteration 2: 157945.836 ns/op
    Iteration 3: 159126.614 ns/op
    Iteration 4: 166451.356 ns/op
    Iteration 5: 160550.279 ns/op

    # Run progress: 75.00% complete, ETA 00:00:15
    # Fork: 2 of 2
    # Warmup Iteration 1: 156200.249 ns/op
    # Warmup Iteration 2: 160897.541 ns/op
    # Warmup Iteration 3: 169560.297 ns/op
    # Warmup Iteration 4: 170340.118 ns/op
    # Warmup Iteration 5: 165995.662 ns/op
    # Warmup Iteration 6: 157818.608 ns/op
    # Warmup Iteration 7: 158936.884 ns/op
    # Warmup Iteration 8: 160276.063 ns/op
    # Warmup Iteration 9: 156367.417 ns/op
    # Warmup Iteration 10: 154037.557 ns/op
    Iteration 1: 153508.761 ns/op
    Iteration 2: 157928.712 ns/op
    Iteration 3: 153968.506 ns/op
    Iteration 4: 153312.665 ns/op
    Iteration 5: 158159.510 ns/op


    Result "testWhile":
    157943.871 ±(99.9%) 5916.507 ns/op [Average]
    (min, avg, max) = (153312.665, 157943.871, 166451.356), stdev = 3913.403
    CI (99.9%): [152027.364, 163860.378] (assumes normal distribution)


    # Run complete. Total time: 00:01:01

    Benchmark Mode Cnt Score Error Units
    TestEquals.testFor avgt 10 131999.918 ± 5188.964 ns/op
    TestEquals.testWhile avgt 10 157943.871 ± 5916.507 ns/op
  6. suzukitadashi created this gist Oct 11, 2016.
    58 changes: 58 additions & 0 deletions TestEquals.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    package jmh18;

    import java.util.concurrent.TimeUnit;

    import org.openjdk.jmh.annotations.Benchmark;
    import org.openjdk.jmh.annotations.BenchmarkMode;
    import org.openjdk.jmh.annotations.Mode;
    import org.openjdk.jmh.annotations.OutputTimeUnit;
    import org.openjdk.jmh.annotations.Scope;
    import org.openjdk.jmh.annotations.State;
    import org.openjdk.jmh.runner.Runner;
    import org.openjdk.jmh.runner.RunnerException;
    import org.openjdk.jmh.runner.options.Options;
    import org.openjdk.jmh.runner.options.OptionsBuilder;

    import obj.TestString;

    @State(Scope.Thread)
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    public class TestEquals {

    public static void main(String[] args) throws RunnerException {
    System.out.println(value1.equalsFor(value2));
    System.out.println(value1.equalsWhile(value2));

    Options opt = new OptionsBuilder().include(TestEquals.class.getSimpleName()).warmupIterations(10)
    .measurementIterations(5).forks(2).build();

    new Runner(opt).run();

    }

    private static TestString value1 = new TestString(new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});
    private static TestString value2 = new TestString(new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'});


    @Benchmark
    public void testFor() {

    for(int i = 0; i < 10000; i++) {
    boolean b = value1.equalsFor(value2);
    }

    }

    @Benchmark
    public void testWhile() {

    for(int i = 0; i < 10000; i++) {
    boolean b = value1.equalsWhile(value2);
    }

    }



    }
    59 changes: 59 additions & 0 deletions TestString.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    package obj;

    import java.util.Arrays;

    public class TestString {

    public TestString() {
    this.value = new char[0];
    }

    public TestString(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
    }

    private final char value[];


    public boolean equalsWhile(Object anObject) {
    if (this == anObject) {
    return true;
    }
    if (anObject instanceof TestString) {
    TestString anotherString = (TestString) anObject;
    int n = value.length;
    if (n == anotherString.value.length) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = 0;
    while (n-- != 0) {
    if (v1[i] != v2[i])
    return false;
    i++;
    }
    return true;
    }
    }
    return false;
    }

    public boolean equalsFor(Object anObject) {
    if (this == anObject) {
    return true;
    }
    if (anObject instanceof TestString) {
    TestString anotherString = (TestString) anObject;
    int n = value.length;
    if (n == anotherString.value.length) {
    char v1[] = value;
    char v2[] = anotherString.value;
    for (int i = 0; i < n; i++) {
    if (v1[i] != v2[i])
    return false;
    }
    return true;
    }
    }
    return false;
    }
    }