2
2
3
3
mod utils;
4
4
5
- use criterion:: { black_box, criterion_group, criterion_main, Criterion } ;
6
5
use modular_bitfield:: {
7
6
bitfield,
8
7
specifiers:: { B12 , B13 , B16 , B3 , B32 , B36 , B4 , B6 , B7 , B8 , B9 } ,
9
8
} ;
10
- use utils:: repeat;
11
-
12
- criterion_group ! ( bench_get, bench_get_variants) ;
13
- criterion_group ! ( bench_set, bench_set_variants) ;
14
- criterion_main ! ( bench_get, bench_set) ;
9
+ use utils:: * ;
15
10
16
11
#[ bitfield]
17
12
pub struct Color {
@@ -87,22 +82,19 @@ pub struct Complex {
87
82
e : B32 , // 4th, .., 7th
88
83
}
89
84
90
- fn bench_set_variants ( c : & mut Criterion ) {
91
- let mut g = c. benchmark_group ( "set_variants" ) ;
92
- g. bench_function ( "Color" , |b| {
93
- let mut input = Color :: new ( ) ;
94
- b. iter ( || {
95
- repeat ( || {
96
- black_box ( & mut input) . set_r ( 1 ) ;
97
- black_box ( & mut input) . set_g ( 1 ) ;
98
- black_box ( & mut input) . set_b ( 1 ) ;
99
- black_box ( & mut input) . set_a ( 1 ) ;
100
- } )
85
+ fn bench_set_variants ( ) {
86
+ one_shot ( "set - Color" , & Color :: new, |mut input| {
87
+ repeat ( || {
88
+ black_box ( & mut input) . set_r ( 1 ) ;
89
+ black_box ( & mut input) . set_g ( 1 ) ;
90
+ black_box ( & mut input) . set_b ( 1 ) ;
91
+ black_box ( & mut input) . set_a ( 1 ) ;
101
92
} ) ;
102
93
} ) ;
103
- g. bench_function ( "SingleBitsInSingleByte" , |b| {
104
- let mut input = SingleBitsInSingleByte :: new ( ) ;
105
- b. iter ( || {
94
+ one_shot (
95
+ "set - SingleBitsInSingleByte" ,
96
+ & SingleBitsInSingleByte :: new,
97
+ |mut input| {
106
98
repeat ( || {
107
99
black_box ( & mut input) . set_b0 ( true ) ;
108
100
black_box ( & mut input) . set_b1 ( true ) ;
@@ -112,105 +104,82 @@ fn bench_set_variants(c: &mut Criterion) {
112
104
black_box ( & mut input) . set_b5 ( true ) ;
113
105
black_box ( & mut input) . set_b6 ( true ) ;
114
106
black_box ( & mut input) . set_b7 ( true ) ;
115
- } )
116
- } ) ;
117
- } ) ;
118
- g. bench_function ( "TwoHalfBytes" , |b| {
119
- let mut input = TwoHalfBytes :: new ( ) ;
120
- b. iter ( || {
121
- repeat ( || {
122
- black_box ( & mut input) . set_h0 ( 1 ) ;
123
- black_box ( & mut input) . set_h1 ( 1 ) ;
124
- } )
107
+ } ) ;
108
+ } ,
109
+ ) ;
110
+ one_shot ( "set - TwoHalfBytes" , & TwoHalfBytes :: new, |mut input| {
111
+ repeat ( || {
112
+ black_box ( & mut input) . set_h0 ( 1 ) ;
113
+ black_box ( & mut input) . set_h1 ( 1 ) ;
125
114
} ) ;
126
115
} ) ;
127
- g. bench_function ( "SingleBitAndRest" , |b| {
128
- let mut input = SingleBitAndRest :: new ( ) ;
129
- b. iter ( || {
116
+ one_shot (
117
+ "set - SingleBitAndRest" ,
118
+ & SingleBitAndRest :: new,
119
+ |mut input| {
130
120
repeat ( || {
131
121
black_box ( & mut input) . set_head ( true ) ;
132
122
black_box ( & mut input) . set_rest ( 1 ) ;
133
- } )
123
+ } ) ;
124
+ } ,
125
+ ) ;
126
+ one_shot ( "set - B7B1" , & B7B1 :: new, |mut input| {
127
+ repeat ( || {
128
+ black_box ( & mut input) . set_b7 ( 1 ) ;
129
+ black_box ( & mut input) . set_b1 ( true ) ;
134
130
} ) ;
135
131
} ) ;
136
- g. bench_function ( "B7B1" , |b| {
137
- let mut input = B7B1 :: new ( ) ;
138
- b. iter ( || {
139
- repeat ( || {
140
- black_box ( & mut input) . set_b7 ( 1 ) ;
141
- black_box ( & mut input) . set_b1 ( true ) ;
142
- } )
132
+ one_shot ( "set - B3B1B4" , & B3B1B4 :: new, |mut input| {
133
+ repeat ( || {
134
+ black_box ( & mut input) . set_b3 ( 1 ) ;
135
+ black_box ( & mut input) . set_b1 ( true ) ;
136
+ black_box ( & mut input) . set_b4 ( 1 ) ;
143
137
} ) ;
144
138
} ) ;
145
- g. bench_function ( "B3B1B4" , |b| {
146
- let mut input = B3B1B4 :: new ( ) ;
147
- b. iter ( || {
148
- repeat ( || {
149
- black_box ( & mut input) . set_b3 ( 1 ) ;
150
- black_box ( & mut input) . set_b1 ( true ) ;
151
- black_box ( & mut input) . set_b4 ( 1 ) ;
152
- } )
139
+ one_shot ( "set - TwoHalfWords" , & TwoHalfWords :: new, |mut input| {
140
+ repeat ( || {
141
+ black_box ( & mut input) . set_fst ( 1 ) ;
142
+ black_box ( & mut input) . set_snd ( 1 ) ;
153
143
} ) ;
154
144
} ) ;
155
- g. bench_function ( "TwoHalfWords" , |b| {
156
- let mut input = TwoHalfWords :: new ( ) ;
157
- b. iter ( || {
158
- repeat ( || {
159
- black_box ( & mut input) . set_fst ( 1 ) ;
160
- black_box ( & mut input) . set_snd ( 1 ) ;
161
- } )
162
- } ) ;
163
- } ) ;
164
- g. bench_function ( "B6B12B6" , |b| {
165
- let mut input = B6B12B6 :: new ( ) ;
166
- b. iter ( || {
167
- repeat ( || {
168
- black_box ( & mut input) . set_front ( 1 ) ;
169
- black_box ( & mut input) . set_middle ( 1 ) ;
170
- black_box ( & mut input) . set_back ( 1 ) ;
171
- } )
145
+ one_shot ( "set - B6B12B6" , & B6B12B6 :: new, |mut input| {
146
+ repeat ( || {
147
+ black_box ( & mut input) . set_front ( 1 ) ;
148
+ black_box ( & mut input) . set_middle ( 1 ) ;
149
+ black_box ( & mut input) . set_back ( 1 ) ;
172
150
} ) ;
173
151
} ) ;
174
- g. bench_function ( "B6B36B6" , |b| {
175
- let mut input = B6B36B6 :: new ( ) ;
176
- b. iter ( || {
177
- repeat ( || {
178
- black_box ( & mut input) . set_front ( 1 ) ;
179
- black_box ( & mut input) . set_middle ( 1 ) ;
180
- black_box ( & mut input) . set_back ( 1 ) ;
181
- } )
152
+ one_shot ( "set - B6B36B6" , & B6B36B6 :: new, |mut input| {
153
+ repeat ( || {
154
+ black_box ( & mut input) . set_front ( 1 ) ;
155
+ black_box ( & mut input) . set_middle ( 1 ) ;
156
+ black_box ( & mut input) . set_back ( 1 ) ;
182
157
} ) ;
183
158
} ) ;
184
- g. bench_function ( "Complex" , |b| {
185
- let mut input = Complex :: new ( ) ;
186
- b. iter ( || {
187
- repeat ( || {
188
- black_box ( & mut input) . set_a ( 1 ) ;
189
- black_box ( & mut input) . set_b ( 1 ) ;
190
- black_box ( & mut input) . set_c ( 1 ) ;
191
- black_box ( & mut input) . set_d ( 1 ) ;
192
- black_box ( & mut input) . set_e ( 1 ) ;
193
- } )
159
+ one_shot ( "set - Complex" , & Complex :: new, |mut input| {
160
+ repeat ( || {
161
+ black_box ( & mut input) . set_a ( 1 ) ;
162
+ black_box ( & mut input) . set_b ( 1 ) ;
163
+ black_box ( & mut input) . set_c ( 1 ) ;
164
+ black_box ( & mut input) . set_d ( 1 ) ;
165
+ black_box ( & mut input) . set_e ( 1 ) ;
194
166
} ) ;
195
167
} ) ;
196
168
}
197
169
198
- fn bench_get_variants ( c : & mut Criterion ) {
199
- let mut g = c. benchmark_group ( "get" ) ;
200
- g. bench_function ( "Color" , |b| {
201
- let input = Color :: new ( ) ;
202
- b. iter ( || {
203
- repeat ( || {
204
- black_box ( input. r ( ) ) ;
205
- black_box ( input. g ( ) ) ;
206
- black_box ( input. b ( ) ) ;
207
- black_box ( input. a ( ) ) ;
208
- } )
170
+ fn bench_get_variants ( ) {
171
+ one_shot ( "get - Color" , & Color :: new, |input| {
172
+ repeat ( || {
173
+ black_box ( input. r ( ) ) ;
174
+ black_box ( input. g ( ) ) ;
175
+ black_box ( input. b ( ) ) ;
176
+ black_box ( input. a ( ) ) ;
209
177
} ) ;
210
178
} ) ;
211
- g. bench_function ( "SingleBitsInSingleByte" , |b| {
212
- let input = SingleBitsInSingleByte :: new ( ) ;
213
- b. iter ( || {
179
+ one_shot (
180
+ "get - SingleBitsInSingleByte" ,
181
+ & SingleBitsInSingleByte :: new,
182
+ |input| {
214
183
repeat ( || {
215
184
black_box ( input. b0 ( ) ) ;
216
185
black_box ( input. b1 ( ) ) ;
@@ -220,85 +189,66 @@ fn bench_get_variants(c: &mut Criterion) {
220
189
black_box ( input. b5 ( ) ) ;
221
190
black_box ( input. b6 ( ) ) ;
222
191
black_box ( input. b7 ( ) ) ;
223
- } )
192
+ } ) ;
193
+ } ,
194
+ ) ;
195
+ one_shot ( "get - TwoHalfBytes" , & TwoHalfBytes :: new, |input| {
196
+ repeat ( || {
197
+ black_box ( input. h0 ( ) ) ;
198
+ black_box ( input. h1 ( ) ) ;
224
199
} ) ;
225
200
} ) ;
226
- g. bench_function ( "TwoHalfBytes" , |b| {
227
- let input = TwoHalfBytes :: new ( ) ;
228
- b. iter ( || {
229
- repeat ( || {
230
- black_box ( input. h0 ( ) ) ;
231
- black_box ( input. h1 ( ) ) ;
232
- } )
201
+ one_shot ( "get - SingleBitAndRest" , & SingleBitAndRest :: new, |input| {
202
+ repeat ( || {
203
+ black_box ( input. head ( ) ) ;
204
+ black_box ( input. rest ( ) ) ;
233
205
} ) ;
234
206
} ) ;
235
- g. bench_function ( "SingleBitAndRest" , |b| {
236
- let input = SingleBitAndRest :: new ( ) ;
237
- b. iter ( || {
238
- repeat ( || {
239
- black_box ( input. head ( ) ) ;
240
- black_box ( input. rest ( ) ) ;
241
- } )
242
- } ) ;
243
- } ) ;
244
- g. bench_function ( "B7B1" , |b| {
245
- let input = B7B1 :: new ( ) ;
246
- b. iter ( || {
247
- repeat ( || {
248
- black_box ( input. b7 ( ) ) ;
249
- black_box ( input. b1 ( ) ) ;
250
- } )
207
+ one_shot ( "get - B7B1" , & B7B1 :: new, |input| {
208
+ repeat ( || {
209
+ black_box ( input. b7 ( ) ) ;
210
+ black_box ( input. b1 ( ) ) ;
251
211
} ) ;
252
212
} ) ;
253
- g. bench_function ( "B3B1B4" , |b| {
254
- let input = B3B1B4 :: new ( ) ;
255
- b. iter ( || {
256
- repeat ( || {
257
- black_box ( input. b3 ( ) ) ;
258
- black_box ( input. b1 ( ) ) ;
259
- black_box ( input. b4 ( ) ) ;
260
- } )
213
+ one_shot ( "get - B3B1B4" , & B3B1B4 :: new, |input| {
214
+ repeat ( || {
215
+ black_box ( input. b3 ( ) ) ;
216
+ black_box ( input. b1 ( ) ) ;
217
+ black_box ( input. b4 ( ) ) ;
261
218
} ) ;
262
219
} ) ;
263
- g. bench_function ( "TwoHalfWords" , |b| {
264
- let input = TwoHalfWords :: new ( ) ;
265
- b. iter ( || {
266
- repeat ( || {
267
- black_box ( input. fst ( ) ) ;
268
- black_box ( input. snd ( ) ) ;
269
- } )
220
+ one_shot ( "get - TwoHalfWords" , & TwoHalfWords :: new, |input| {
221
+ repeat ( || {
222
+ black_box ( input. fst ( ) ) ;
223
+ black_box ( input. snd ( ) ) ;
270
224
} ) ;
271
225
} ) ;
272
- g. bench_function ( "B6B12B6" , |b| {
273
- let input = B6B12B6 :: new ( ) ;
274
- b. iter ( || {
275
- repeat ( || {
276
- black_box ( input. front ( ) ) ;
277
- black_box ( input. middle ( ) ) ;
278
- black_box ( input. back ( ) ) ;
279
- } )
226
+ one_shot ( "get - B6B12B6" , & B6B12B6 :: new, |input| {
227
+ repeat ( || {
228
+ black_box ( input. front ( ) ) ;
229
+ black_box ( input. middle ( ) ) ;
230
+ black_box ( input. back ( ) ) ;
280
231
} ) ;
281
232
} ) ;
282
- g. bench_function ( "B6B36B6" , |b| {
283
- let input = B6B36B6 :: new ( ) ;
284
- b. iter ( || {
285
- repeat ( || {
286
- black_box ( input. front ( ) ) ;
287
- black_box ( input. middle ( ) ) ;
288
- black_box ( input. back ( ) ) ;
289
- } )
233
+ one_shot ( "get - B6B36B6" , & B6B36B6 :: new, |input| {
234
+ repeat ( || {
235
+ black_box ( input. front ( ) ) ;
236
+ black_box ( input. middle ( ) ) ;
237
+ black_box ( input. back ( ) ) ;
290
238
} ) ;
291
239
} ) ;
292
- g. bench_function ( "Complex" , |b| {
293
- let input = Complex :: new ( ) ;
294
- b. iter ( || {
295
- repeat ( || {
296
- black_box ( input. a ( ) ) ;
297
- black_box ( input. b ( ) ) ;
298
- black_box ( input. c ( ) ) ;
299
- black_box ( input. d ( ) ) ;
300
- black_box ( input. e ( ) ) ;
301
- } )
240
+ one_shot ( "get - Complex" , & Complex :: new, |input| {
241
+ repeat ( || {
242
+ black_box ( input. a ( ) ) ;
243
+ black_box ( input. b ( ) ) ;
244
+ black_box ( input. c ( ) ) ;
245
+ black_box ( input. d ( ) ) ;
246
+ black_box ( input. e ( ) ) ;
302
247
} ) ;
303
248
} ) ;
304
249
}
250
+
251
+ fn main ( ) {
252
+ bench_set_variants ( ) ;
253
+ bench_get_variants ( ) ;
254
+ }
0 commit comments