1 /* 2 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package micro.benchmarks; 26 27 import java.util.concurrent.TimeUnit; 28 29 import org.openjdk.jmh.annotations.Benchmark; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Scope; 32 import org.openjdk.jmh.annotations.State; 33 import org.openjdk.jmh.annotations.Warmup; 34 35 /** 36 * Benchmarks cost of {@link String#indexOf(int)} and {@link String#indexOf(String)}. 37 */ 38 @OutputTimeUnit(TimeUnit.MILLISECONDS) 39 public class StringBenchmark extends BenchmarkBase { 40 41 @State(Scope.Benchmark) 42 public static class BenchState { 43 char ch1 = 'Q'; 44 String ch1string = "Q"; 45 char ch2 = 'X'; 46 String s1 = "Qu"; 47 String s2 = "ne"; 48 49 String longString; 50 51 public BenchState() { 52 String str = "ab"; 53 for (int i = 0; i < 15; i++) { 54 str = str + str; 55 } 56 longString = str + "xx"; 57 } 58 59 // Checkstyle: stop 60 String lorem = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; 61 String loremLastChar = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum?"; 62 // Checkstyle: resume 63 64 String smallLorem = lorem.substring(0, 13); 65 String largeLorem = lorem.concat(lorem); 66 67 char[] smallCharArray = lorem.substring(0, 13).toCharArray(); 68 char[] largeCharArray = lorem.concat(lorem).toCharArray(); 69 } 70 71 @Benchmark 72 @Warmup(iterations = 5) 73 public int indexOfChar(BenchState state) { 74 return state.lorem.indexOf(state.ch1); 75 } 76 77 @Benchmark 78 @Warmup(iterations = 5) 79 public int indexOfSingleCharString(BenchState state) { 80 return state.lorem.indexOf(state.ch1string); 81 } 82 83 @Benchmark 84 @Warmup(iterations = 5) 85 public int indexOfSingleCharStringLong(BenchState state) { 86 return state.longString.indexOf('x'); 87 } 88 89 @Benchmark 90 @Warmup(iterations = 5) 91 public int indexOfCharNotFound(BenchState state) { 92 return state.lorem.indexOf(state.ch2); 93 } 94 95 @Benchmark 96 @Warmup(iterations = 5) 97 public int indexOfString(BenchState state) { 98 return state.lorem.indexOf(state.s1); 99 } 100 101 @Benchmark 102 @Warmup(iterations = 5) 103 public int indexOfStringNotFound(BenchState state) { 104 return state.lorem.indexOf(state.s2); 105 } 106 107 @Benchmark 108 @Warmup(iterations = 5) 109 public int compareTo(BenchState state) { 110 return state.lorem.compareTo(state.loremLastChar); 111 } 112 113 @Benchmark 114 @Warmup(iterations = 5) 115 public String compressSmallString(BenchState state) { 116 return new String(state.smallCharArray); 117 } 118 119 @Benchmark 120 @Warmup(iterations = 5) 121 public String compressLargeString(BenchState state) { 122 return new String(state.largeCharArray); 123 } 124 125 @Benchmark 126 @Warmup(iterations = 5) 127 public char[] inflateSmallString(BenchState state) { 128 return state.smallLorem.toCharArray(); 129 } 130 131 @Benchmark 132 @Warmup(iterations = 5) 133 public char[] inflateLargeString(BenchState state) { 134 return state.largeLorem.toCharArray(); 135 } 136 }