1 /* 2 * Copyright (c) 2014 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.java.lang; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Mode; 30 import org.openjdk.jmh.annotations.OutputTimeUnit; 31 import org.openjdk.jmh.annotations.Scope; 32 import org.openjdk.jmh.annotations.Setup; 33 import org.openjdk.jmh.annotations.State; 34 35 import java.util.concurrent.TimeUnit; 36 37 @BenchmarkMode(Mode.AverageTime) 38 @OutputTimeUnit(TimeUnit.NANOSECONDS) 39 @State(Scope.Thread) 40 public class StringIndexOf { 41 42 private String dataString; 43 private String searchString; 44 private String dataStringBig; 45 private String searchStringBig; 46 private String data; 47 private String sub; 48 private String shortSub1; 49 private String data2; 50 private String shortSub2; 51 52 @Setup 53 public void setup() { 54 dataString = "ngdfilsoscargfdgf"; 55 searchString = "oscar"; 56 dataStringBig = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkjas"; 57 searchStringBig = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalk"; 58 data = "0000100101010010110101010010101110101001110110101010010101010010000010111010101010101010100010010101110111010101101010100010010100001010111111100001010101001010100001010101001010101010111010010101010101010101010101010"; 59 sub = "10101010"; 60 shortSub1 = "1"; 61 data2 = "00001001010100a10110101010010101110101001110110101010010101010010000010111010101010101010a100010010101110111010101101010100010010a100a0010101111111000010101010010101000010101010010101010101110a10010101010101010101010101010"; 62 shortSub2 = "a"; 63 } 64 65 /** IndexOf Micros Strings */ 66 67 /** 68 * Benchmarks String.indexOf with a rather small String to search and a rather small String to search for. The 69 * searched string contains the string that is searched for. 70 */ 71 @Benchmark 72 public int success() { 73 return dataString.indexOf(searchString, 2); 74 } 75 76 /** 77 * Benchmarks String.indexOf with a rather big String to search and a rather big String to search for. The searched 78 * string contains the string that is searched for. 79 */ 80 @Benchmark 81 public int successBig() { 82 return dataStringBig.indexOf(searchStringBig, 2); 83 } 84 85 /** 86 * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 8 chars and most 87 * oftenly will require a inner lopp match in String.indexOf with sse42. 88 */ 89 @Benchmark 90 public int advancedWithMediumSub() { 91 int index = 0; 92 int dummy = 0; 93 while ((index = data.indexOf(sub, index)) > -1) { 94 index++; 95 dummy += index; 96 } 97 return dummy; 98 } 99 100 101 /** 102 * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars will find a 103 * huge amount of matches 104 */ 105 @Benchmark 106 public int advancedWithShortSub1() { 107 int dummy = 0; 108 int index = 0; 109 while ((index = data.indexOf(shortSub1, index)) > -1) { 110 index++; 111 dummy += index; 112 } 113 return dummy; 114 } 115 116 117 /** 118 * Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars but only with 119 * a few matches. 120 */ 121 @Benchmark 122 public int advancedWithShortSub2() { 123 int dummy = 0; 124 int index = 0; 125 while ((index = data2.indexOf(shortSub2, index)) > -1) { 126 index++; 127 dummy += index; 128 } 129 return dummy; 130 } 131 132 @Benchmark 133 public void constantPattern() { 134 String tmp = "simple-hash:SHA-1/UTF-8"; 135 if (!tmp.contains("SHA-1")) { 136 throw new RuntimeException("indexOf failed"); 137 } 138 } 139 140 }