package org.openjdk.jmh.samples; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Setup; 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 java.util.concurrent.TimeUnit; import java.nio.*; import java.util.*; import java.util.concurrent.*; @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) public class JMHSample_97_ArrayEqual { @State(Scope.Benchmark) public static class BenchmarkState { String[] str1 = new String[500]; String[] str2 = new String[500]; String[] str3 = new String[500]; byte[][] b1 = new byte[500][]; byte[][] b2 = new byte[500][]; byte[][] b3 = new byte[500][]; char[][] c1 = new char[500][]; char[][] c2 = new char[500][]; char[][] c3 = new char[500][]; //String base = "abcdefgh"; String base = ""; //String base = "abcdefghijabcdefghijabcdefghija"; //String base = "abcdefghijabcdefghijabcdefghijab"; // 32 @Setup public void setup() { /*base = base + base; base = base + base; base = base + base; // 256 base = base + base; // 512 base = base + base;*/ for (int i = 0; i < 500; i++) { if (i % 8 == 0){ str1[i] = base + (10000000+i); str2[i] = base + (10000000+i); str3[i] = base + (10000001+334*i); } else if (i % 8 == 1) { str1[i] = base + (1000000+i); str2[i] = base + (1000000+i); str3[i] = base + (1000001+334*i); } else if (i % 8 == 2) { str1[i] = base + (100000+i); str2[i] = base + (100000+i); str3[i] = base + (100001+34*i); } else if (i % 8 == 3){ str1[i] = base + (10000+i); str2[i] = base + (10000+i); str3[i] = base + (10001+34*i); } else if (i % 8 == 4){ str1[i] = base + (1000+i); str2[i] = base + (1000+i); str3[i] = base + (1001+2*i); } else if (i % 8 == 5) { int v = 105+2*i; if (v >= 1000) {v = v -500;} str1[i] = base + (100+i); str2[i] = base + (100+i); str3[i] = base + (v); } else if (i % 8 == 6) { int v = (i%100)+10; if (v > 99) {v = v-50;} str1[i] = base + (v); str2[i] = base + (v); str3[i] = base + (v+1); } else { int v = i%10; if (v == 0 ) {v = 1;} if (v == 9 ) {v = 8;} str1[i] = base + (v); str2[i] = base + (v); str3[i] = base + (v+1); } } for (int i =0; i < 500; i++) { c1[i] = str1[i].toCharArray(); c2[i] = str2[i].toCharArray(); c3[i] = str3[i].toCharArray(); b1[i] = str1[i].getBytes(); b2[i] = str2[i].getBytes(); b3[i] = str3[i].getBytes(); } } } @Benchmark public int string_equal(BenchmarkState state) { int count = 0; String[] str1 = state.str1; String[] str2 = state.str2; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (str1[i].equals(str2[i]) == true) count++; } } return count; } @Benchmark public int string_not_equal(BenchmarkState state) { int count = 0; String[] str1 = state.str1; String[] str3 = state.str3; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (str1[i].equals(str3[i]) == false) count++; } } return count; } @Benchmark public int byte_equal(BenchmarkState state) { int count = 0; byte[][] b1 = state.b1; byte[][] b2 = state.b2; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (Arrays.equals(b1[i], b2[i]) == true) count++; } } return count; } @Benchmark public int byte_not_equal(BenchmarkState state) { int count = 0; byte[][] b1 = state.b1; byte[][] b3 = state.b3; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (Arrays.equals(b1[i], b3[i]) == false) count++; } } return count; } @Benchmark public int char_equal(BenchmarkState state) { int count = 0; char[][] c1 = state.c1; char[][] c2 = state.c2; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (Arrays.equals(c1[i], c2[i]) == true) count++; } } return count; } @Benchmark public int char_not_equal(BenchmarkState state) { int count = 0; char[][] c1 = state.c1; char[][] c3 = state.c3; for (int j = 0; j < 500; j++) { for (int i =0; i < 500; i++) { if (Arrays.equals(c1[i], c3[i]) == false) count++; } } return count; } public static void main(String[] args) throws RunnerException { Options opt = new OptionsBuilder() .include(".*" + JMHSample_97_ArrayEqual.class.getSimpleName() + ".*") .warmupIterations(5) .measurementIterations(5) .forks(1) .build(); new Runner(opt).run(); } }