Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringIndexOf.java
41161 views
1
/*
2
* Copyright (c) 2014, 2019, 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
package org.openjdk.bench.java.lang;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Mode;
28
import org.openjdk.jmh.annotations.OutputTimeUnit;
29
import org.openjdk.jmh.annotations.Scope;
30
import org.openjdk.jmh.annotations.Setup;
31
import org.openjdk.jmh.annotations.State;
32
33
import java.util.concurrent.TimeUnit;
34
35
@BenchmarkMode(Mode.AverageTime)
36
@OutputTimeUnit(TimeUnit.NANOSECONDS)
37
@State(Scope.Thread)
38
public class StringIndexOf {
39
40
private String dataString;
41
private String searchString;
42
private String dataStringBig;
43
private String searchStringBig;
44
private String data;
45
private String sub;
46
private String shortSub1;
47
private String data2;
48
private String shortSub2;
49
private String string16Short;
50
private String string16Medium;
51
private String string16Long;
52
private char searchChar;
53
private char searchChar16;
54
private String searchString16;
55
56
@Setup
57
public void setup() {
58
dataString = "ngdfilsoscargfdgf";
59
searchString = "oscar";
60
dataStringBig = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkjas";
61
searchStringBig = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalk";
62
data = "0000100101010010110101010010101110101001110110101010010101010010000010111010101010101010100010010101110111010101101010100010010100001010111111100001010101001010100001010101001010101010111010010101010101010101010101010";
63
sub = "10101010";
64
shortSub1 = "1";
65
data2 = "00001001010100a10110101010010101110101001110110101010010101010010000010111010101010101010a100010010101110111010101101010100010010a100a0010101111111000010101010010101000010101010010101010101110a10010101010101010101010101010";
66
shortSub2 = "a";
67
searchChar = 's';
68
69
string16Short = "scar\u01fe1";
70
string16Medium = "capaapapapasdkajdlkajskldjaslkjdlkasjdsalksca1r\u01fescar";
71
string16Long = "2937489745890797905764956790452976742965790437698498409583479067ngdcapaapapapasdkajdlkajskldjaslkjdlkasjdsalkja1sscar\u01fescar";
72
searchChar16 = 0x1fe;
73
searchString16 = "\u01fe";
74
}
75
76
77
/** IndexOf Micros Chars */
78
@Benchmark
79
public int searchCharLongSuccess() {
80
return dataStringBig.indexOf(searchChar);
81
}
82
83
@Benchmark
84
public int searchCharMediumSuccess() {
85
return searchStringBig.indexOf(searchChar);
86
}
87
88
@Benchmark
89
public int searchCharShortSuccess() {
90
return searchString.indexOf(searchChar);
91
}
92
93
@Benchmark
94
public int searchChar16LongSuccess() {
95
return string16Long.indexOf(searchChar16);
96
}
97
98
@Benchmark
99
public int searchChar16MediumSuccess() {
100
return string16Medium.indexOf(searchChar16);
101
}
102
103
@Benchmark
104
public int searchChar16ShortSuccess() {
105
return string16Short.indexOf(searchChar16);
106
}
107
108
@Benchmark
109
public int searchChar16LongWithOffsetSuccess() {
110
return string16Long.indexOf(searchChar16, 3);
111
}
112
113
@Benchmark
114
public int searchChar16MediumWithOffsetSuccess() {
115
return string16Medium.indexOf(searchChar16, 3);
116
}
117
118
@Benchmark
119
public int searchChar16ShortWithOffsetSuccess() {
120
return string16Short.indexOf(searchChar16, 2);
121
}
122
123
/** IndexOf Micros Strings */
124
@Benchmark
125
public int searchString16LongLatinSuccess() {
126
return string16Long.indexOf(shortSub1);
127
}
128
129
@Benchmark
130
public int searchString16MediumLatinSuccess() {
131
return string16Medium.indexOf(shortSub1);
132
}
133
134
@Benchmark
135
public int searchString16ShortLatinSuccess() {
136
return string16Long.indexOf(shortSub2);
137
}
138
139
@Benchmark
140
public int searchString16LongWithOffsetLatinSuccess() {
141
return string16Long.indexOf(shortSub1, 3);
142
}
143
144
@Benchmark
145
public int searchString16MediumWithOffsetLatinSuccess() {
146
return string16Medium.indexOf(shortSub1, 3);
147
}
148
149
@Benchmark
150
public int searchString16ShortWithOffsetLatinSuccess() {
151
return string16Short.indexOf(shortSub2, 1);
152
}
153
154
@Benchmark
155
public int searchString16LongWithOffsetSuccess() {
156
return string16Long.indexOf(searchString16, 3);
157
}
158
159
@Benchmark
160
public int searchString16MediumWithOffsetSuccess() {
161
return string16Medium.indexOf(searchString16, 3);
162
}
163
164
@Benchmark
165
public int searchString16ShortWithOffsetSuccess() {
166
return string16Short.indexOf(searchString16, 2);
167
}
168
169
@Benchmark
170
public int searchString16LongSuccess() {
171
return string16Long.indexOf(searchString16);
172
}
173
174
@Benchmark
175
public int searchString16MediumSuccess() {
176
return string16Medium.indexOf(searchString16);
177
}
178
179
@Benchmark
180
public int searchString16ShortSuccess() {
181
return string16Short.indexOf(searchString16);
182
}
183
184
/**
185
* Benchmarks String.indexOf with a rather small String to search and a rather small String to search for. The
186
* searched string contains the string that is searched for.
187
*/
188
@Benchmark
189
public int success() {
190
return dataString.indexOf(searchString, 2);
191
}
192
193
/**
194
* Benchmarks String.indexOf with a rather big String to search and a rather big String to search for. The searched
195
* string contains the string that is searched for.
196
*/
197
@Benchmark
198
public int successBig() {
199
return dataStringBig.indexOf(searchStringBig, 2);
200
}
201
202
/**
203
* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 8 chars and most
204
* oftenly will require a inner lopp match in String.indexOf with sse42.
205
*/
206
@Benchmark
207
public int advancedWithMediumSub() {
208
int index = 0;
209
int dummy = 0;
210
while ((index = data.indexOf(sub, index)) > -1) {
211
index++;
212
dummy += index;
213
}
214
return dummy;
215
}
216
217
218
/**
219
* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars will find a
220
* huge amount of matches
221
*/
222
@Benchmark
223
public int advancedWithShortSub1() {
224
int dummy = 0;
225
int index = 0;
226
while ((index = data.indexOf(shortSub1, index)) > -1) {
227
index++;
228
dummy += index;
229
}
230
return dummy;
231
}
232
233
234
/**
235
* Benchmarks String.indexOf with a rather big String. Search repeatedly for a matched that is 1 chars but only with
236
* a few matches.
237
*/
238
@Benchmark
239
public int advancedWithShortSub2() {
240
int dummy = 0;
241
int index = 0;
242
while ((index = data2.indexOf(shortSub2, index)) > -1) {
243
index++;
244
dummy += index;
245
}
246
return dummy;
247
}
248
249
@Benchmark
250
public void constantPattern() {
251
String tmp = "simple-hash:SHA-1/UTF-8";
252
if (!tmp.contains("SHA-1")) {
253
throw new RuntimeException("indexOf failed");
254
}
255
}
256
257
}
258
259