2525import java .util .Random ;
2626import org .openjdk .jmh .annotations .Benchmark ;
2727import org .openjdk .jmh .annotations .BenchmarkMode ;
28+ import org .openjdk .jmh .annotations .Param ;
2829import org .openjdk .jmh .annotations .OutputTimeUnit ;
2930import org .openjdk .jmh .annotations .Mode ;
31+ import org .openjdk .jmh .annotations .Setup ;
3032import org .openjdk .jmh .annotations .Scope ;
3133import org .openjdk .jmh .annotations .State ;
34+ import org .openjdk .jmh .infra .Blackhole ;
3235
3336import java .util .concurrent .TimeUnit ;
3437
4144@ OutputTimeUnit (TimeUnit .NANOSECONDS )
4245@ State (Scope .Thread )
4346public 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+
5678 for (int i = 0 ; i < pathCnt ; i ++) {
5779 latn1_short [i ] = makeRndString (false , 15 );
5880 latn1_sse4 [i ] = makeRndString (false , 16 );
@@ -65,7 +87,7 @@ public class StringIndexOfChar {
6587 }
6688 }
6789
68- private static String makeRndString (boolean isUtf16 , int length ) {
90+ private String makeRndString (boolean isUtf16 , int length ) {
6991 StringBuilder sb = new StringBuilder (length );
7092 if (length > 0 ){
7193 sb .append (isUtf16 ?'\u2026' :'b' ); // ...
@@ -81,141 +103,116 @@ private static String makeRndString(boolean isUtf16, int length) {
81103
82104
83105 @ Benchmark
84- public void latin1_mixed_char () {
85- int ret = 0 ;
106+ public void latin1_mixed_char (Blackhole bh ) {
86107 for (String what : latn1_mixedLength ) {
87- ret += what .indexOf ('a' );
108+ bh . consume ( what .indexOf ('a' ) );
88109 }
89110 }
90111
91112 @ Benchmark
92- public void utf16_mixed_char () {
93- int ret = 0 ;
113+ public void utf16_mixed_char (Blackhole bh ) {
94114 for (String what : utf16_mixedLength ) {
95- ret += what .indexOf ('a' );
115+ bh . consume ( what .indexOf ('a' ) );
96116 }
97117 }
98118
99119 @ Benchmark
100- public void latin1_mixed_String () {
101- int ret = 0 ;
120+ public void latin1_mixed_String (Blackhole bh ) {
102121 for (String what : latn1_mixedLength ) {
103- ret += what .indexOf ("a" );
122+ bh . consume ( what .indexOf ("a" ) );
104123 }
105124 }
106125
107126 @ Benchmark
108- public void utf16_mixed_String () {
109- int ret = 0 ;
127+ public void utf16_mixed_String (Blackhole bh ) {
110128 for (String what : utf16_mixedLength ) {
111- ret += what .indexOf ("a" );
129+ bh . consume ( what .indexOf ("a" ) );
112130 }
113131 }
114132
115133 ////////// more detailed code path dependent tests //////////
116134
117135 @ Benchmark
118- public void latin1_Short_char () {
119- int ret = 0 ;
136+ public void latin1_Short_char (Blackhole bh ) {
120137 for (String what : latn1_short ) {
121- ret += what .indexOf ('a' );
138+ bh . consume ( what .indexOf ('a' ) );
122139 }
123140 }
124141
125142 @ Benchmark
126- public void latin1_SSE4_char () {
127- int ret = 0 ;
143+ public void latin1_SSE4_char (Blackhole bh ) {
128144 for (String what : latn1_sse4 ) {
129- ret += what .indexOf ('a' );
145+ bh . consume ( what .indexOf ('a' ) );
130146 }
131147 }
132148
133149 @ Benchmark
134- public void latin1_AVX2_char () {
135- int ret = 0 ;
150+ public void latin1_AVX2_char (Blackhole bh ) {
136151 for (String what : latn1_avx2 ) {
137- ret += what .indexOf ('a' );
152+ bh . consume ( what .indexOf ('a' ) );
138153 }
139154 }
140155
141156 @ Benchmark
142- public int utf16_Short_char () {
143- int ret = 0 ;
157+ public void utf16_Short_char (Blackhole bh ) {
144158 for (String what : utf16_short ) {
145- ret += what .indexOf ('a' );
159+ bh . consume ( what .indexOf ('a' ) );
146160 }
147- return ret ;
148161 }
149162
150163 @ Benchmark
151- public int utf16_SSE4_char () {
152- int ret = 0 ;
164+ public void utf16_SSE4_char (Blackhole bh ) {
153165 for (String what : utf16_sse4 ) {
154- ret += what .indexOf ('a' );
166+ bh . consume ( what .indexOf ('a' ) );
155167 }
156- return ret ;
157168 }
158169
159170 @ Benchmark
160- public int utf16_AVX2_char () {
161- int ret = 0 ;
171+ public void utf16_AVX2_char (Blackhole bh ) {
162172 for (String what : utf16_avx2 ) {
163- ret += what .indexOf ('a' );
173+ bh . consume ( what .indexOf ('a' ) );
164174 }
165- return ret ;
166175 }
167176
168177 @ Benchmark
169- public int latin1_Short_String () {
170- int ret = 0 ;
178+ public void latin1_Short_String (Blackhole bh ) {
171179 for (String what : latn1_short ) {
172- ret += what .indexOf ("a" );
180+ bh . consume ( what .indexOf ("a" ) );
173181 }
174- return ret ;
175182 }
176183
177184 @ Benchmark
178- public int latin1_SSE4_String () {
179- int ret = 0 ;
185+ public void latin1_SSE4_String (Blackhole bh ) {
180186 for (String what : latn1_sse4 ) {
181- ret += what .indexOf ("a" );
187+ bh . consume ( what .indexOf ("a" ) );
182188 }
183- return ret ;
184189 }
185190
186191 @ Benchmark
187- public int latin1_AVX2_String () {
188- int ret = 0 ;
192+ public void latin1_AVX2_String (Blackhole bh ) {
189193 for (String what : latn1_avx2 ) {
190- ret += what .indexOf ("a" );
194+ bh . consume ( what .indexOf ("a" ) );
191195 }
192- return ret ;
193196 }
194197
195198 @ Benchmark
196- public int utf16_Short_String () {
197- int ret = 0 ;
199+ public void utf16_Short_String (Blackhole bh ) {
198200 for (String what : utf16_short ) {
199- ret += what .indexOf ("a" );
201+ bh . consume ( what .indexOf ("a" ) );
200202 }
201- return ret ;
202203 }
203204
204205 @ Benchmark
205- public int utf16_SSE4_String () {
206- int ret = 0 ;
206+ public void utf16_SSE4_String (Blackhole bh ) {
207207 for (String what : utf16_sse4 ) {
208- ret += what .indexOf ("a" );
208+ bh . consume ( what .indexOf ("a" ) );
209209 }
210- return ret ;
211210 }
212211
213212 @ Benchmark
214- public int utf16_AVX2_String () {
215- int ret = 0 ;
213+ public void utf16_AVX2_String (Blackhole bh ) {
216214 for (String what : utf16_avx2 ) {
217- ret += what .indexOf ("a" );
215+ bh . consume ( what .indexOf ("a" ) );
218216 }
219- return ret ;
220217 }
221218}
0 commit comments