Skip to content

Commit f1b9b12

Browse files
committed
add iterator benchmarks for for _ in loops so we can bench the desugaring
1 parent f6dea97 commit f1b9b12

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

library/core/benches/iter.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,23 @@ fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
146146
/// Helper to benchmark `sum` for iterators taken by value which
147147
/// can optimize `fold`, and by reference which cannot.
148148
macro_rules! bench_sums {
149-
($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
149+
($bench_sum:ident, $bench_sum_loop:ident, $bench_ref_sum:ident, $iter:expr) => {
150150
#[bench]
151151
fn $bench_sum(b: &mut Bencher) {
152152
b.iter(|| -> i64 { $iter.map(black_box).sum() });
153153
}
154154

155+
#[bench]
156+
fn $bench_sum_loop(b: &mut Bencher) {
157+
b.iter(|| -> i64 {
158+
let mut acc = 0;
159+
for i in $iter.map(black_box) {
160+
acc += i;
161+
}
162+
acc
163+
});
164+
}
165+
155166
#[bench]
156167
fn $bench_ref_sum(b: &mut Bencher) {
157168
b.iter(|| -> i64 { $iter.map(black_box).by_ref().sum() });
@@ -161,138 +172,161 @@ macro_rules! bench_sums {
161172

162173
bench_sums! {
163174
bench_flat_map_sum,
175+
bench_flat_map_sum_loop,
164176
bench_flat_map_ref_sum,
165177
(0i64..1000).flat_map(|x| x..x+1000)
166178
}
167179

168180
bench_sums! {
169181
bench_flat_map_chain_sum,
182+
bench_flat_map_chain_sum_loop,
170183
bench_flat_map_chain_ref_sum,
171184
(0i64..1000000).flat_map(|x| once(x).chain(once(x)))
172185
}
173186

174187
bench_sums! {
175188
bench_enumerate_sum,
189+
bench_enumerate_sum_loop,
176190
bench_enumerate_ref_sum,
177191
(0i64..1000000).enumerate().map(|(i, x)| x * i as i64)
178192
}
179193

180194
bench_sums! {
181195
bench_enumerate_chain_sum,
196+
bench_enumerate_chain_sum_loop,
182197
bench_enumerate_chain_ref_sum,
183198
(0i64..1000000).chain(0..1000000).enumerate().map(|(i, x)| x * i as i64)
184199
}
185200

186201
bench_sums! {
187202
bench_filter_sum,
203+
bench_filter_sum_loop,
188204
bench_filter_ref_sum,
189205
(0i64..1000000).filter(|x| x % 3 == 0)
190206
}
191207

192208
bench_sums! {
193209
bench_filter_chain_sum,
210+
bench_filter_chain_sum_loop,
194211
bench_filter_chain_ref_sum,
195212
(0i64..1000000).chain(0..1000000).filter(|x| x % 3 == 0)
196213
}
197214

198215
bench_sums! {
199216
bench_filter_map_sum,
217+
bench_filter_map_sum_loop,
200218
bench_filter_map_ref_sum,
201219
(0i64..1000000).filter_map(|x| x.checked_mul(x))
202220
}
203221

204222
bench_sums! {
205223
bench_filter_map_chain_sum,
224+
bench_filter_map_chain_sum_loop,
206225
bench_filter_map_chain_ref_sum,
207226
(0i64..1000000).chain(0..1000000).filter_map(|x| x.checked_mul(x))
208227
}
209228

210229
bench_sums! {
211230
bench_fuse_sum,
231+
bench_fuse_sum_loop,
212232
bench_fuse_ref_sum,
213233
(0i64..1000000).fuse()
214234
}
215235

216236
bench_sums! {
217237
bench_fuse_chain_sum,
238+
bench_fuse_chain_sum_loop,
218239
bench_fuse_chain_ref_sum,
219240
(0i64..1000000).chain(0..1000000).fuse()
220241
}
221242

222243
bench_sums! {
223244
bench_inspect_sum,
245+
bench_inspect_sum_loop,
224246
bench_inspect_ref_sum,
225247
(0i64..1000000).inspect(|_| {})
226248
}
227249

228250
bench_sums! {
229251
bench_inspect_chain_sum,
252+
bench_inspect_chain_sum_loop,
230253
bench_inspect_chain_ref_sum,
231254
(0i64..1000000).chain(0..1000000).inspect(|_| {})
232255
}
233256

234257
bench_sums! {
235258
bench_peekable_sum,
259+
bench_peekable_sum_loop,
236260
bench_peekable_ref_sum,
237261
(0i64..1000000).peekable()
238262
}
239263

240264
bench_sums! {
241265
bench_peekable_chain_sum,
266+
bench_peekable_chain_sum_loop,
242267
bench_peekable_chain_ref_sum,
243268
(0i64..1000000).chain(0..1000000).peekable()
244269
}
245270

246271
bench_sums! {
247272
bench_skip_sum,
273+
bench_skip_sum_loop,
248274
bench_skip_ref_sum,
249275
(0i64..1000000).skip(1000)
250276
}
251277

252278
bench_sums! {
253279
bench_skip_chain_sum,
280+
bench_skip_chain_sum_loop,
254281
bench_skip_chain_ref_sum,
255282
(0i64..1000000).chain(0..1000000).skip(1000)
256283
}
257284

258285
bench_sums! {
259286
bench_skip_while_sum,
287+
bench_skip_while_sum_loop,
260288
bench_skip_while_ref_sum,
261289
(0i64..1000000).skip_while(|&x| x < 1000)
262290
}
263291

264292
bench_sums! {
265293
bench_skip_while_chain_sum,
294+
bench_skip_while_chain_sum_loop,
266295
bench_skip_while_chain_ref_sum,
267296
(0i64..1000000).chain(0..1000000).skip_while(|&x| x < 1000)
268297
}
269298

270299
bench_sums! {
271300
bench_take_while_chain_sum,
301+
bench_take_while_chain_sum_loop,
272302
bench_take_while_chain_ref_sum,
273303
(0i64..1000000).chain(1000000..).take_while(|&x| x < 1111111)
274304
}
275305

276306
bench_sums! {
277307
bench_cycle_take_sum,
308+
bench_cycle_take_sum_loop,
278309
bench_cycle_take_ref_sum,
279310
(0..10000).cycle().take(1000000)
280311
}
281312

282313
bench_sums! {
283314
bench_cycle_skip_take_sum,
315+
bench_cycle_skip_take_sum_loop,
284316
bench_cycle_skip_take_ref_sum,
285317
(0..100000).cycle().skip(1000000).take(1000000)
286318
}
287319

288320
bench_sums! {
289321
bench_cycle_take_skip_sum,
322+
bench_cycle_take_skip_sum_loop,
290323
bench_cycle_take_skip_ref_sum,
291324
(0..100000).cycle().take(1000000).skip(100000)
292325
}
293326

294327
bench_sums! {
295328
bench_skip_cycle_skip_zip_add_sum,
329+
bench_skip_cycle_skip_zip_add_sum_loop,
296330
bench_skip_cycle_skip_zip_add_ref_sum,
297331
(0..100000).skip(100).cycle().skip(100)
298332
.zip((0..100000).cycle().skip(10))
@@ -368,6 +402,26 @@ fn bench_lt(b: &mut Bencher) {
368402
b.iter(|| (0..100000).map(black_box).lt((0..100000).map(black_box)))
369403
}
370404

405+
#[bench]
406+
fn bench_desugar(b: &mut Bencher) {
407+
let vec1: Vec<_> = (0usize..100000).collect();
408+
let vec2 = black_box(vec1.clone());
409+
b.iter(|| {
410+
let iter = vec1
411+
.iter()
412+
.enumerate()
413+
.map(|(idx, e)| idx.wrapping_add(*e))
414+
.zip(vec2.iter())
415+
.map(|(a, b)| a.wrapping_add(*b))
416+
.fuse();
417+
let mut acc = 0;
418+
for i in iter {
419+
acc += i;
420+
}
421+
acc
422+
})
423+
}
424+
371425
#[bench]
372426
fn bench_trusted_random_access_adapters(b: &mut Bencher) {
373427
let vec1: Vec<_> = (0usize..100000).collect();

0 commit comments

Comments
 (0)