Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringIndexOfChar.java
41161 views
/*1* Copyright Amazon.com Inc. 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 java.util.Random;25import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.Param;28import org.openjdk.jmh.annotations.OutputTimeUnit;29import org.openjdk.jmh.annotations.Mode;30import org.openjdk.jmh.annotations.Setup;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.State;33import org.openjdk.jmh.infra.Blackhole;3435import java.util.concurrent.TimeUnit;3637/**38* This benchmark can be used to measure performance between StringLatin1 and StringUTF16 in terms of39* performance of the indexOf(char) and indexOf(String) methods which are intrinsified.40* On x86 the behaviour of the indexOf method is contingent upon the length of the string41*/42@BenchmarkMode(Mode.AverageTime)43@OutputTimeUnit(TimeUnit.NANOSECONDS)44@State(Scope.Thread)45public class StringIndexOfChar {46@Param("100000")47private int loops;4849@Param("1000")50private int pathCnt;5152@Param("1999")53private int rngSeed;5455private Random rng;56private String[] latn1_short;57private String[] latn1_sse4;58private String[] latn1_avx2;59private String[] latn1_mixedLength;60private String[] utf16_short;61private String[] utf16_sse4;62private String[] utf16_avx2;63private String[] utf16_mixedLength;6465@Setup66public void setup() {67rng = new Random(rngSeed);68latn1_short = new String[pathCnt];69latn1_sse4 = new String[pathCnt];70latn1_avx2 = new String[pathCnt];71latn1_mixedLength = new String[pathCnt];72utf16_short = new String[pathCnt];73utf16_sse4 = new String[pathCnt];74utf16_avx2 = new String[pathCnt];75utf16_mixedLength = new String[pathCnt];7677for (int i = 0; i < pathCnt; i++) {78latn1_short[i] = makeRndString(false, 15);79latn1_sse4[i] = makeRndString(false, 16);80latn1_avx2[i] = makeRndString(false, 32);81utf16_short[i] = makeRndString(true, 7);82utf16_sse4[i] = makeRndString(true, 8);83utf16_avx2[i] = makeRndString(true, 16);84latn1_mixedLength[i] = makeRndString(false, rng.nextInt(65));85utf16_mixedLength[i] = makeRndString(true, rng.nextInt(65));86}87}8889private String makeRndString(boolean isUtf16, int length) {90StringBuilder sb = new StringBuilder(length);91if(length > 0){92sb.append(isUtf16?'\u2026':'b'); // ...9394for (int i = 1; i < length-1; i++) {95sb.append((char)('b' + rng.nextInt(26)));96}9798sb.append(rng.nextInt(3) >= 1?'a':'b');//66.6% of time 'a' is in string99}100return sb.toString();101}102103104@Benchmark105public void latin1_mixed_char(Blackhole bh) {106for (String what : latn1_mixedLength) {107bh.consume(what.indexOf('a'));108}109}110111@Benchmark112public void utf16_mixed_char(Blackhole bh) {113for (String what : utf16_mixedLength) {114bh.consume(what.indexOf('a'));115}116}117118@Benchmark119public void latin1_mixed_String(Blackhole bh) {120for (String what : latn1_mixedLength) {121bh.consume(what.indexOf("a"));122}123}124125@Benchmark126public void utf16_mixed_String(Blackhole bh) {127for (String what : utf16_mixedLength) {128bh.consume(what.indexOf("a"));129}130}131132////////// more detailed code path dependent tests //////////133134@Benchmark135public void latin1_Short_char(Blackhole bh) {136for (String what : latn1_short) {137bh.consume(what.indexOf('a'));138}139}140141@Benchmark142public void latin1_SSE4_char(Blackhole bh) {143for (String what : latn1_sse4) {144bh.consume(what.indexOf('a'));145}146}147148@Benchmark149public void latin1_AVX2_char(Blackhole bh) {150for (String what : latn1_avx2) {151bh.consume(what.indexOf('a'));152}153}154155@Benchmark156public void utf16_Short_char(Blackhole bh) {157for (String what : utf16_short) {158bh.consume(what.indexOf('a'));159}160}161162@Benchmark163public void utf16_SSE4_char(Blackhole bh) {164for (String what : utf16_sse4) {165bh.consume(what.indexOf('a'));166}167}168169@Benchmark170public void utf16_AVX2_char(Blackhole bh) {171for (String what : utf16_avx2) {172bh.consume(what.indexOf('a'));173}174}175176@Benchmark177public void latin1_Short_String(Blackhole bh) {178for (String what : latn1_short) {179bh.consume(what.indexOf("a"));180}181}182183@Benchmark184public void latin1_SSE4_String(Blackhole bh) {185for (String what : latn1_sse4) {186bh.consume(what.indexOf("a"));187}188}189190@Benchmark191public void latin1_AVX2_String(Blackhole bh) {192for (String what : latn1_avx2) {193bh.consume(what.indexOf("a"));194}195}196197@Benchmark198public void utf16_Short_String(Blackhole bh) {199for (String what : utf16_short) {200bh.consume(what.indexOf("a"));201}202}203204@Benchmark205public void utf16_SSE4_String(Blackhole bh) {206for (String what : utf16_sse4) {207bh.consume(what.indexOf("a"));208}209}210211@Benchmark212public void utf16_AVX2_String(Blackhole bh) {213for (String what : utf16_avx2) {214bh.consume(what.indexOf("a"));215}216}217}218219220