Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringIndexOfChar.java
41161 views
1
/*
2
* Copyright Amazon.com Inc. 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
package org.openjdk.bench.java.lang;
24
25
import java.util.Random;
26
import org.openjdk.jmh.annotations.Benchmark;
27
import org.openjdk.jmh.annotations.BenchmarkMode;
28
import org.openjdk.jmh.annotations.Param;
29
import org.openjdk.jmh.annotations.OutputTimeUnit;
30
import org.openjdk.jmh.annotations.Mode;
31
import org.openjdk.jmh.annotations.Setup;
32
import org.openjdk.jmh.annotations.Scope;
33
import org.openjdk.jmh.annotations.State;
34
import org.openjdk.jmh.infra.Blackhole;
35
36
import java.util.concurrent.TimeUnit;
37
38
/**
39
* This benchmark can be used to measure performance between StringLatin1 and StringUTF16 in terms of
40
* performance of the indexOf(char) and indexOf(String) methods which are intrinsified.
41
* On x86 the behaviour of the indexOf method is contingent upon the length of the string
42
*/
43
@BenchmarkMode(Mode.AverageTime)
44
@OutputTimeUnit(TimeUnit.NANOSECONDS)
45
@State(Scope.Thread)
46
public class StringIndexOfChar {
47
@Param("100000")
48
private int loops;
49
50
@Param("1000")
51
private int pathCnt;
52
53
@Param("1999")
54
private int rngSeed;
55
56
private Random rng;
57
private String[] latn1_short;
58
private String[] latn1_sse4;
59
private String[] latn1_avx2;
60
private String[] latn1_mixedLength;
61
private String[] utf16_short;
62
private String[] utf16_sse4;
63
private String[] utf16_avx2;
64
private String[] utf16_mixedLength;
65
66
@Setup
67
public void setup() {
68
rng = new Random(rngSeed);
69
latn1_short = new String[pathCnt];
70
latn1_sse4 = new String[pathCnt];
71
latn1_avx2 = new String[pathCnt];
72
latn1_mixedLength = new String[pathCnt];
73
utf16_short = new String[pathCnt];
74
utf16_sse4 = new String[pathCnt];
75
utf16_avx2 = new String[pathCnt];
76
utf16_mixedLength = new String[pathCnt];
77
78
for (int i = 0; i < pathCnt; i++) {
79
latn1_short[i] = makeRndString(false, 15);
80
latn1_sse4[i] = makeRndString(false, 16);
81
latn1_avx2[i] = makeRndString(false, 32);
82
utf16_short[i] = makeRndString(true, 7);
83
utf16_sse4[i] = makeRndString(true, 8);
84
utf16_avx2[i] = makeRndString(true, 16);
85
latn1_mixedLength[i] = makeRndString(false, rng.nextInt(65));
86
utf16_mixedLength[i] = makeRndString(true, rng.nextInt(65));
87
}
88
}
89
90
private String makeRndString(boolean isUtf16, int length) {
91
StringBuilder sb = new StringBuilder(length);
92
if(length > 0){
93
sb.append(isUtf16?'\u2026':'b'); // ...
94
95
for (int i = 1; i < length-1; i++) {
96
sb.append((char)('b' + rng.nextInt(26)));
97
}
98
99
sb.append(rng.nextInt(3) >= 1?'a':'b');//66.6% of time 'a' is in string
100
}
101
return sb.toString();
102
}
103
104
105
@Benchmark
106
public void latin1_mixed_char(Blackhole bh) {
107
for (String what : latn1_mixedLength) {
108
bh.consume(what.indexOf('a'));
109
}
110
}
111
112
@Benchmark
113
public void utf16_mixed_char(Blackhole bh) {
114
for (String what : utf16_mixedLength) {
115
bh.consume(what.indexOf('a'));
116
}
117
}
118
119
@Benchmark
120
public void latin1_mixed_String(Blackhole bh) {
121
for (String what : latn1_mixedLength) {
122
bh.consume(what.indexOf("a"));
123
}
124
}
125
126
@Benchmark
127
public void utf16_mixed_String(Blackhole bh) {
128
for (String what : utf16_mixedLength) {
129
bh.consume(what.indexOf("a"));
130
}
131
}
132
133
////////// more detailed code path dependent tests //////////
134
135
@Benchmark
136
public void latin1_Short_char(Blackhole bh) {
137
for (String what : latn1_short) {
138
bh.consume(what.indexOf('a'));
139
}
140
}
141
142
@Benchmark
143
public void latin1_SSE4_char(Blackhole bh) {
144
for (String what : latn1_sse4) {
145
bh.consume(what.indexOf('a'));
146
}
147
}
148
149
@Benchmark
150
public void latin1_AVX2_char(Blackhole bh) {
151
for (String what : latn1_avx2) {
152
bh.consume(what.indexOf('a'));
153
}
154
}
155
156
@Benchmark
157
public void utf16_Short_char(Blackhole bh) {
158
for (String what : utf16_short) {
159
bh.consume(what.indexOf('a'));
160
}
161
}
162
163
@Benchmark
164
public void utf16_SSE4_char(Blackhole bh) {
165
for (String what : utf16_sse4) {
166
bh.consume(what.indexOf('a'));
167
}
168
}
169
170
@Benchmark
171
public void utf16_AVX2_char(Blackhole bh) {
172
for (String what : utf16_avx2) {
173
bh.consume(what.indexOf('a'));
174
}
175
}
176
177
@Benchmark
178
public void latin1_Short_String(Blackhole bh) {
179
for (String what : latn1_short) {
180
bh.consume(what.indexOf("a"));
181
}
182
}
183
184
@Benchmark
185
public void latin1_SSE4_String(Blackhole bh) {
186
for (String what : latn1_sse4) {
187
bh.consume(what.indexOf("a"));
188
}
189
}
190
191
@Benchmark
192
public void latin1_AVX2_String(Blackhole bh) {
193
for (String what : latn1_avx2) {
194
bh.consume(what.indexOf("a"));
195
}
196
}
197
198
@Benchmark
199
public void utf16_Short_String(Blackhole bh) {
200
for (String what : utf16_short) {
201
bh.consume(what.indexOf("a"));
202
}
203
}
204
205
@Benchmark
206
public void utf16_SSE4_String(Blackhole bh) {
207
for (String what : utf16_sse4) {
208
bh.consume(what.indexOf("a"));
209
}
210
}
211
212
@Benchmark
213
public void utf16_AVX2_String(Blackhole bh) {
214
for (String what : utf16_avx2) {
215
bh.consume(what.indexOf("a"));
216
}
217
}
218
}
219
220