25
25
import java .util .Random ;
26
26
import org .openjdk .jmh .annotations .Benchmark ;
27
27
import org .openjdk .jmh .annotations .BenchmarkMode ;
28
+ import org .openjdk .jmh .annotations .Param ;
28
29
import org .openjdk .jmh .annotations .OutputTimeUnit ;
29
30
import org .openjdk .jmh .annotations .Mode ;
31
+ import org .openjdk .jmh .annotations .Setup ;
30
32
import org .openjdk .jmh .annotations .Scope ;
31
33
import org .openjdk .jmh .annotations .State ;
34
+ import org .openjdk .jmh .infra .Blackhole ;
32
35
33
36
import java .util .concurrent .TimeUnit ;
34
37
41
44
@ OutputTimeUnit (TimeUnit .NANOSECONDS )
42
45
@ State (Scope .Thread )
43
46
public class StringIndexOfChar {
44
- private static final int loops = 100000 ;
45
- private static final Random rng = new Random (1999 );
46
- private static final int pathCnt = 1000 ;
47
- private static final String [] latn1_short = new String [pathCnt ];
48
- private static final String [] latn1_sse4 = new String [pathCnt ];
49
- private static final String [] latn1_avx2 = new String [pathCnt ];
50
- private static final String [] latn1_mixedLength = new String [pathCnt ];
51
- private static final String [] utf16_short = new String [pathCnt ];
52
- private static final String [] utf16_sse4 = new String [pathCnt ];
53
- private static final String [] utf16_avx2 = new String [pathCnt ];
54
- private static final String [] utf16_mixedLength = new String [pathCnt ];
55
- static {
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
+
56
78
for (int i = 0 ; i < pathCnt ; i ++) {
57
79
latn1_short [i ] = makeRndString (false , 15 );
58
80
latn1_sse4 [i ] = makeRndString (false , 16 );
@@ -65,7 +87,7 @@ public class StringIndexOfChar {
65
87
}
66
88
}
67
89
68
- private static String makeRndString (boolean isUtf16 , int length ) {
90
+ private String makeRndString (boolean isUtf16 , int length ) {
69
91
StringBuilder sb = new StringBuilder (length );
70
92
if (length > 0 ){
71
93
sb .append (isUtf16 ?'\u2026' :'b' ); // ...
@@ -81,141 +103,116 @@ private static String makeRndString(boolean isUtf16, int length) {
81
103
82
104
83
105
@ Benchmark
84
- public void latin1_mixed_char () {
85
- int ret = 0 ;
106
+ public void latin1_mixed_char (Blackhole bh ) {
86
107
for (String what : latn1_mixedLength ) {
87
- ret += what .indexOf ('a' );
108
+ bh . consume ( what .indexOf ('a' ) );
88
109
}
89
110
}
90
111
91
112
@ Benchmark
92
- public void utf16_mixed_char () {
93
- int ret = 0 ;
113
+ public void utf16_mixed_char (Blackhole bh ) {
94
114
for (String what : utf16_mixedLength ) {
95
- ret += what .indexOf ('a' );
115
+ bh . consume ( what .indexOf ('a' ) );
96
116
}
97
117
}
98
118
99
119
@ Benchmark
100
- public void latin1_mixed_String () {
101
- int ret = 0 ;
120
+ public void latin1_mixed_String (Blackhole bh ) {
102
121
for (String what : latn1_mixedLength ) {
103
- ret += what .indexOf ("a" );
122
+ bh . consume ( what .indexOf ("a" ) );
104
123
}
105
124
}
106
125
107
126
@ Benchmark
108
- public void utf16_mixed_String () {
109
- int ret = 0 ;
127
+ public void utf16_mixed_String (Blackhole bh ) {
110
128
for (String what : utf16_mixedLength ) {
111
- ret += what .indexOf ("a" );
129
+ bh . consume ( what .indexOf ("a" ) );
112
130
}
113
131
}
114
132
115
133
////////// more detailed code path dependent tests //////////
116
134
117
135
@ Benchmark
118
- public void latin1_Short_char () {
119
- int ret = 0 ;
136
+ public void latin1_Short_char (Blackhole bh ) {
120
137
for (String what : latn1_short ) {
121
- ret += what .indexOf ('a' );
138
+ bh . consume ( what .indexOf ('a' ) );
122
139
}
123
140
}
124
141
125
142
@ Benchmark
126
- public void latin1_SSE4_char () {
127
- int ret = 0 ;
143
+ public void latin1_SSE4_char (Blackhole bh ) {
128
144
for (String what : latn1_sse4 ) {
129
- ret += what .indexOf ('a' );
145
+ bh . consume ( what .indexOf ('a' ) );
130
146
}
131
147
}
132
148
133
149
@ Benchmark
134
- public void latin1_AVX2_char () {
135
- int ret = 0 ;
150
+ public void latin1_AVX2_char (Blackhole bh ) {
136
151
for (String what : latn1_avx2 ) {
137
- ret += what .indexOf ('a' );
152
+ bh . consume ( what .indexOf ('a' ) );
138
153
}
139
154
}
140
155
141
156
@ Benchmark
142
- public int utf16_Short_char () {
143
- int ret = 0 ;
157
+ public void utf16_Short_char (Blackhole bh ) {
144
158
for (String what : utf16_short ) {
145
- ret += what .indexOf ('a' );
159
+ bh . consume ( what .indexOf ('a' ) );
146
160
}
147
- return ret ;
148
161
}
149
162
150
163
@ Benchmark
151
- public int utf16_SSE4_char () {
152
- int ret = 0 ;
164
+ public void utf16_SSE4_char (Blackhole bh ) {
153
165
for (String what : utf16_sse4 ) {
154
- ret += what .indexOf ('a' );
166
+ bh . consume ( what .indexOf ('a' ) );
155
167
}
156
- return ret ;
157
168
}
158
169
159
170
@ Benchmark
160
- public int utf16_AVX2_char () {
161
- int ret = 0 ;
171
+ public void utf16_AVX2_char (Blackhole bh ) {
162
172
for (String what : utf16_avx2 ) {
163
- ret += what .indexOf ('a' );
173
+ bh . consume ( what .indexOf ('a' ) );
164
174
}
165
- return ret ;
166
175
}
167
176
168
177
@ Benchmark
169
- public int latin1_Short_String () {
170
- int ret = 0 ;
178
+ public void latin1_Short_String (Blackhole bh ) {
171
179
for (String what : latn1_short ) {
172
- ret += what .indexOf ("a" );
180
+ bh . consume ( what .indexOf ("a" ) );
173
181
}
174
- return ret ;
175
182
}
176
183
177
184
@ Benchmark
178
- public int latin1_SSE4_String () {
179
- int ret = 0 ;
185
+ public void latin1_SSE4_String (Blackhole bh ) {
180
186
for (String what : latn1_sse4 ) {
181
- ret += what .indexOf ("a" );
187
+ bh . consume ( what .indexOf ("a" ) );
182
188
}
183
- return ret ;
184
189
}
185
190
186
191
@ Benchmark
187
- public int latin1_AVX2_String () {
188
- int ret = 0 ;
192
+ public void latin1_AVX2_String (Blackhole bh ) {
189
193
for (String what : latn1_avx2 ) {
190
- ret += what .indexOf ("a" );
194
+ bh . consume ( what .indexOf ("a" ) );
191
195
}
192
- return ret ;
193
196
}
194
197
195
198
@ Benchmark
196
- public int utf16_Short_String () {
197
- int ret = 0 ;
199
+ public void utf16_Short_String (Blackhole bh ) {
198
200
for (String what : utf16_short ) {
199
- ret += what .indexOf ("a" );
201
+ bh . consume ( what .indexOf ("a" ) );
200
202
}
201
- return ret ;
202
203
}
203
204
204
205
@ Benchmark
205
- public int utf16_SSE4_String () {
206
- int ret = 0 ;
206
+ public void utf16_SSE4_String (Blackhole bh ) {
207
207
for (String what : utf16_sse4 ) {
208
- ret += what .indexOf ("a" );
208
+ bh . consume ( what .indexOf ("a" ) );
209
209
}
210
- return ret ;
211
210
}
212
211
213
212
@ Benchmark
214
- public int utf16_AVX2_String () {
215
- int ret = 0 ;
213
+ public void utf16_AVX2_String (Blackhole bh ) {
216
214
for (String what : utf16_avx2 ) {
217
- ret += what .indexOf ("a" );
215
+ bh . consume ( what .indexOf ("a" ) );
218
216
}
219
- return ret ;
220
217
}
221
218
}
0 commit comments