Skip to content

Instantly share code, notes, and snippets.

@suzukitadashi
Last active October 11, 2016 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzukitadashi/b0adad3a2de7e6d784c26c8af7486211 to your computer and use it in GitHub Desktop.
Save suzukitadashi/b0adad3a2de7e6d784c26c8af7486211 to your computer and use it in GitHub Desktop.
Equalsメソッドの速度確認
package jmh18;
import org.openjdk.jmh.annotations.Benchmark;
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;
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();
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);
}
}
@Benchmark
public void testWhile2() {
for(int i = 0; i < 10000; i++) {
boolean b = value1.equalsWhile2(value2);
}
}
}
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 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) {
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;
}
}
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:00
# Fork: 1 of 2
# 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:47
# Fork: 2 of 2
# 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":
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)
# 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: 50.00% complete, ETA 00:00:31
# Fork: 1 of 2
# 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: 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":
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:02
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment