Skip to content

Commit 80be3ed

Browse files
committed
add external iteration version of iter benchmarks
The by_ref versions used to exercise external iteration but since the try_fold optimiations this is no loner the case.
1 parent 74a85b5 commit 80be3ed

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

library/core/benches/iter.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
201201
/// Helper to benchmark `sum` for iterators taken by value which
202202
/// can optimize `fold`, and by reference which cannot.
203203
macro_rules! bench_sums {
204-
($bench_sum:ident, $bench_ref_sum:ident, $iter:expr) => {
204+
($bench_sum:ident, $bench_ref_sum:ident, $bench_ext_sum:ident, $iter:expr) => {
205205
#[bench]
206206
fn $bench_sum(b: &mut Bencher) {
207207
b.iter(|| -> i64 { $iter.map(black_box).sum() });
@@ -211,144 +211,178 @@ macro_rules! bench_sums {
211211
fn $bench_ref_sum(b: &mut Bencher) {
212212
b.iter(|| -> i64 { $iter.map(black_box).by_ref().sum() });
213213
}
214+
215+
#[bench]
216+
fn $bench_ext_sum(b: &mut Bencher) {
217+
b.iter(|| -> i64 {
218+
let mut sum = 0;
219+
for i in $iter.map(black_box) {
220+
sum += i;
221+
}
222+
sum
223+
});
224+
}
214225
};
215226
}
216227

217228
bench_sums! {
218229
bench_flat_map_sum,
219230
bench_flat_map_ref_sum,
231+
bench_flat_map_ext_sum,
220232
(0i64..1000).flat_map(|x| x..x+1000)
221233
}
222234

223235
bench_sums! {
224236
bench_flat_map_chain_sum,
225237
bench_flat_map_chain_ref_sum,
238+
bench_flat_map_chain_ext_sum,
226239
(0i64..1000000).flat_map(|x| once(x).chain(once(x)))
227240
}
228241

229242
bench_sums! {
230243
bench_enumerate_sum,
231244
bench_enumerate_ref_sum,
245+
bench_enumerate_ext_sum,
232246
(0i64..1000000).enumerate().map(|(i, x)| x * i as i64)
233247
}
234248

235249
bench_sums! {
236250
bench_enumerate_chain_sum,
237251
bench_enumerate_chain_ref_sum,
252+
bench_enumerate_chain_ext_sum,
238253
(0i64..1000000).chain(0..1000000).enumerate().map(|(i, x)| x * i as i64)
239254
}
240255

241256
bench_sums! {
242257
bench_filter_sum,
243258
bench_filter_ref_sum,
259+
bench_filter_ext_sum,
244260
(0i64..1000000).filter(|x| x % 3 == 0)
245261
}
246262

247263
bench_sums! {
248264
bench_filter_chain_sum,
249265
bench_filter_chain_ref_sum,
266+
bench_filter_chain_ext_sum,
250267
(0i64..1000000).chain(0..1000000).filter(|x| x % 3 == 0)
251268
}
252269

253270
bench_sums! {
254271
bench_filter_map_sum,
255272
bench_filter_map_ref_sum,
273+
bench_filter_map_ext_sum,
256274
(0i64..1000000).filter_map(|x| x.checked_mul(x))
257275
}
258276

259277
bench_sums! {
260278
bench_filter_map_chain_sum,
261279
bench_filter_map_chain_ref_sum,
280+
bench_filter_map_chain_ext_sum,
262281
(0i64..1000000).chain(0..1000000).filter_map(|x| x.checked_mul(x))
263282
}
264283

265284
bench_sums! {
266285
bench_fuse_sum,
267286
bench_fuse_ref_sum,
287+
bench_fuse_ext_sum,
268288
(0i64..1000000).fuse()
269289
}
270290

271291
bench_sums! {
272292
bench_fuse_chain_sum,
273293
bench_fuse_chain_ref_sum,
294+
bench_fuse_chain_ext_sum,
274295
(0i64..1000000).chain(0..1000000).fuse()
275296
}
276297

277298
bench_sums! {
278299
bench_inspect_sum,
279300
bench_inspect_ref_sum,
301+
bench_inspect_ext_sum,
280302
(0i64..1000000).inspect(|_| {})
281303
}
282304

283305
bench_sums! {
284306
bench_inspect_chain_sum,
285307
bench_inspect_chain_ref_sum,
308+
bench_inspect_chain_ext_sum,
286309
(0i64..1000000).chain(0..1000000).inspect(|_| {})
287310
}
288311

289312
bench_sums! {
290313
bench_peekable_sum,
291314
bench_peekable_ref_sum,
315+
bench_peekable_ext_sum,
292316
(0i64..1000000).peekable()
293317
}
294318

295319
bench_sums! {
296320
bench_peekable_chain_sum,
297321
bench_peekable_chain_ref_sum,
322+
bench_peekable_chain_ext_sum,
298323
(0i64..1000000).chain(0..1000000).peekable()
299324
}
300325

301326
bench_sums! {
302327
bench_skip_sum,
303328
bench_skip_ref_sum,
329+
bench_skip_ext_sum,
304330
(0i64..1000000).skip(1000)
305331
}
306332

307333
bench_sums! {
308334
bench_skip_chain_sum,
309335
bench_skip_chain_ref_sum,
336+
bench_skip_chain_ext_sum,
310337
(0i64..1000000).chain(0..1000000).skip(1000)
311338
}
312339

313340
bench_sums! {
314341
bench_skip_while_sum,
315342
bench_skip_while_ref_sum,
343+
bench_skip_while_ext_sum,
316344
(0i64..1000000).skip_while(|&x| x < 1000)
317345
}
318346

319347
bench_sums! {
320348
bench_skip_while_chain_sum,
321349
bench_skip_while_chain_ref_sum,
350+
bench_skip_while_chain_ext_sum,
322351
(0i64..1000000).chain(0..1000000).skip_while(|&x| x < 1000)
323352
}
324353

325354
bench_sums! {
326355
bench_take_while_chain_sum,
327356
bench_take_while_chain_ref_sum,
357+
bench_take_while_chain_ext_sum,
328358
(0i64..1000000).chain(1000000..).take_while(|&x| x < 1111111)
329359
}
330360

331361
bench_sums! {
332362
bench_cycle_take_sum,
333363
bench_cycle_take_ref_sum,
364+
bench_cycle_take_ext_sum,
334365
(0..10000).cycle().take(1000000)
335366
}
336367

337368
bench_sums! {
338369
bench_cycle_skip_take_sum,
339370
bench_cycle_skip_take_ref_sum,
371+
bench_cycle_skip_take_ext_sum,
340372
(0..100000).cycle().skip(1000000).take(1000000)
341373
}
342374

343375
bench_sums! {
344376
bench_cycle_take_skip_sum,
345377
bench_cycle_take_skip_ref_sum,
378+
bench_cycle_take_skip_ext_sum,
346379
(0..100000).cycle().take(1000000).skip(100000)
347380
}
348381

349382
bench_sums! {
350383
bench_skip_cycle_skip_zip_add_sum,
351384
bench_skip_cycle_skip_zip_add_ref_sum,
385+
bench_skip_cycle_skip_zip_add_ext_sum,
352386
(0..100000).skip(100).cycle().skip(100)
353387
.zip((0..100000).cycle().skip(10))
354388
.map(|(a,b)| a+b)
@@ -359,6 +393,7 @@ bench_sums! {
359393
bench_sums! {
360394
bench_slice_chain_sum,
361395
bench_slice_chain_ref_sum,
396+
bench_slice_chain_ext_sum,
362397
(&[0; 512]).iter().chain((&[1; 512]).iter())
363398
}
364399

0 commit comments

Comments
 (0)