Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringIndexOf.java
41161 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.lang;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;3132import java.util.concurrent.TimeUnit;3334@BenchmarkMode(Mode.AverageTime)35@OutputTimeUnit(TimeUnit.NANOSECONDS)36@State(Scope.Thread)37public class StringIndexOf {3839private String dataString;40private String searchString;41private String dataStringBig;42private String searchStringBig;43private String data;44private String sub;45private String shortSub1;46private String data2;47private String shortSub2;48private String string16Short;49private String string16Medium;50private String string16Long;51private char searchChar;52private char searchChar16;53private String searchString16;5455@Setup56public void setup() {57dataString = "ngdfilsoscargfdgf";58searchString = "oscar";59dataStringBig = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkjas";60searchStringBig = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalk";61data = "0000100101010010110101010010101110101001110110101010010101010010000010111010101010101010100010010101110111010101101010100010010100001010111111100001010101001010100001010101001010101010111010010101010101010101010101010";62sub = "10101010";63shortSub1 = "1";64data2 = "00001001010100a10110101010010101110101001110110101010010101010010000010111010101010101010a100010010101110111010101101010100010010a100a0010101111111000010101010010101000010101010010101010101110a10010101010101010101010101010";65shortSub2 = "a";66searchChar = 's';6768string16Short = "scar\u01fe1";69string16Medium = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalksca1r\u01fescar";70string16Long = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkja1sscar\u01fescar";71searchChar16 = 0x1fe;72searchString16 = "\u01fe";73}747576/** IndexOf Micros Chars */77@Benchmark78public int searchCharLongSuccess() {79return dataStringBig.indexOf(searchChar);80}8182@Benchmark83public int searchCharMediumSuccess() {84return searchStringBig.indexOf(searchChar);85}8687@Benchmark88public int searchCharShortSuccess() {89return searchString.indexOf(searchChar);90}9192@Benchmark93public int searchChar16LongSuccess() {94return string16Long.indexOf(searchChar16);95}9697@Benchmark98public int searchChar16MediumSuccess() {99return string16Medium.indexOf(searchChar16);100}101102@Benchmark103public int searchChar16ShortSuccess() {104return string16Short.indexOf(searchChar16);105}106107@Benchmark108public int searchChar16LongWithOffsetSuccess() {109return string16Long.indexOf(searchChar16, 3);110}111112@Benchmark113public int searchChar16MediumWithOffsetSuccess() {114return string16Medium.indexOf(searchChar16, 3);115}116117@Benchmark118public int searchChar16ShortWithOffsetSuccess() {119return string16Short.indexOf(searchChar16, 2);120}121122/** IndexOf Micros Strings */123@Benchmark124public int searchString16LongLatinSuccess() {125return string16Long.indexOf(shortSub1);126}127128@Benchmark129public int searchString16MediumLatinSuccess() {130return string16Medium.indexOf(shortSub1);131}132133@Benchmark134public int searchString16ShortLatinSuccess() {135return string16Long.indexOf(shortSub2);136}137138@Benchmark139public int searchString16LongWithOffsetLatinSuccess() {140return string16Long.indexOf(shortSub1, 3);141}142143@Benchmark144public int searchString16MediumWithOffsetLatinSuccess() {145return string16Medium.indexOf(shortSub1, 3);146}147148@Benchmark149public int searchString16ShortWithOffsetLatinSuccess() {150return string16Short.indexOf(shortSub2, 1);151}152153@Benchmark154public int searchString16LongWithOffsetSuccess() {155return string16Long.indexOf(searchString16, 3);156}157158@Benchmark159public int searchString16MediumWithOffsetSuccess() {160return string16Medium.indexOf(searchString16, 3);161}162163@Benchmark164public int searchString16ShortWithOffsetSuccess() {165return string16Short.indexOf(searchString16, 2);166}167168@Benchmark169public int searchString16LongSuccess() {170return string16Long.indexOf(searchString16);171}172173@Benchmark174public int searchString16MediumSuccess() {175return string16Medium.indexOf(searchString16);176}177178@Benchmark179public int searchString16ShortSuccess() {180return string16Short.indexOf(searchString16);181}182183/**184* Benchmarks String.indexOf with a rather small String to search and a rather small String to search for. The185* searched string contains the string that is searched for.186*/187@Benchmark188public int success() {189return dataString.indexOf(searchString, 2);190}191192/**193* Benchmarks String.indexOf with a rather big String to search and a rather big String to search for. The searched194* string contains the string that is searched for.195*/196@Benchmark197public int successBig() {198return dataStringBig.indexOf(searchStringBig, 2);199}200201/**202* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 8 chars and most203* oftenly will require a inner lopp match in String.indexOf with sse42.204*/205@Benchmark206public int advancedWithMediumSub() {207int index = 0;208int dummy = 0;209while ((index = data.indexOf(sub, index)) > -1) {210index++;211dummy += index;212}213return dummy;214}215216217/**218* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars will find a219* huge amount of matches220*/221@Benchmark222public int advancedWithShortSub1() {223int dummy = 0;224int index = 0;225while ((index = data.indexOf(shortSub1, index)) > -1) {226index++;227dummy += index;228}229return dummy;230}231232233/**234* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars but only with235* a few matches.236*/237@Benchmark238public int advancedWithShortSub2() {239int dummy = 0;240int index = 0;241while ((index = data2.indexOf(shortSub2, index)) > -1) {242index++;243dummy += index;244}245return dummy;246}247248@Benchmark249public void constantPattern() {250String tmp = "simple-hash:SHA-1/UTF-8";251if (!tmp.contains("SHA-1")) {252throw new RuntimeException("indexOf failed");253}254}255256}257258259