@@ -201,7 +201,7 @@ fn bench_for_each_chain_ref_fold(b: &mut Bencher) {
201
201
/// Helper to benchmark `sum` for iterators taken by value which
202
202
/// can optimize `fold`, and by reference which cannot.
203
203
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) => {
205
205
#[ bench]
206
206
fn $bench_sum( b: & mut Bencher ) {
207
207
b. iter( || -> i64 { $iter. map( black_box) . sum( ) } ) ;
@@ -211,144 +211,178 @@ macro_rules! bench_sums {
211
211
fn $bench_ref_sum( b: & mut Bencher ) {
212
212
b. iter( || -> i64 { $iter. map( black_box) . by_ref( ) . sum( ) } ) ;
213
213
}
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
+ }
214
225
} ;
215
226
}
216
227
217
228
bench_sums ! {
218
229
bench_flat_map_sum,
219
230
bench_flat_map_ref_sum,
231
+ bench_flat_map_ext_sum,
220
232
( 0i64 ..1000 ) . flat_map( |x| x..x+1000 )
221
233
}
222
234
223
235
bench_sums ! {
224
236
bench_flat_map_chain_sum,
225
237
bench_flat_map_chain_ref_sum,
238
+ bench_flat_map_chain_ext_sum,
226
239
( 0i64 ..1000000 ) . flat_map( |x| once( x) . chain( once( x) ) )
227
240
}
228
241
229
242
bench_sums ! {
230
243
bench_enumerate_sum,
231
244
bench_enumerate_ref_sum,
245
+ bench_enumerate_ext_sum,
232
246
( 0i64 ..1000000 ) . enumerate( ) . map( |( i, x) | x * i as i64 )
233
247
}
234
248
235
249
bench_sums ! {
236
250
bench_enumerate_chain_sum,
237
251
bench_enumerate_chain_ref_sum,
252
+ bench_enumerate_chain_ext_sum,
238
253
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . enumerate( ) . map( |( i, x) | x * i as i64 )
239
254
}
240
255
241
256
bench_sums ! {
242
257
bench_filter_sum,
243
258
bench_filter_ref_sum,
259
+ bench_filter_ext_sum,
244
260
( 0i64 ..1000000 ) . filter( |x| x % 3 == 0 )
245
261
}
246
262
247
263
bench_sums ! {
248
264
bench_filter_chain_sum,
249
265
bench_filter_chain_ref_sum,
266
+ bench_filter_chain_ext_sum,
250
267
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . filter( |x| x % 3 == 0 )
251
268
}
252
269
253
270
bench_sums ! {
254
271
bench_filter_map_sum,
255
272
bench_filter_map_ref_sum,
273
+ bench_filter_map_ext_sum,
256
274
( 0i64 ..1000000 ) . filter_map( |x| x. checked_mul( x) )
257
275
}
258
276
259
277
bench_sums ! {
260
278
bench_filter_map_chain_sum,
261
279
bench_filter_map_chain_ref_sum,
280
+ bench_filter_map_chain_ext_sum,
262
281
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . filter_map( |x| x. checked_mul( x) )
263
282
}
264
283
265
284
bench_sums ! {
266
285
bench_fuse_sum,
267
286
bench_fuse_ref_sum,
287
+ bench_fuse_ext_sum,
268
288
( 0i64 ..1000000 ) . fuse( )
269
289
}
270
290
271
291
bench_sums ! {
272
292
bench_fuse_chain_sum,
273
293
bench_fuse_chain_ref_sum,
294
+ bench_fuse_chain_ext_sum,
274
295
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . fuse( )
275
296
}
276
297
277
298
bench_sums ! {
278
299
bench_inspect_sum,
279
300
bench_inspect_ref_sum,
301
+ bench_inspect_ext_sum,
280
302
( 0i64 ..1000000 ) . inspect( |_| { } )
281
303
}
282
304
283
305
bench_sums ! {
284
306
bench_inspect_chain_sum,
285
307
bench_inspect_chain_ref_sum,
308
+ bench_inspect_chain_ext_sum,
286
309
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . inspect( |_| { } )
287
310
}
288
311
289
312
bench_sums ! {
290
313
bench_peekable_sum,
291
314
bench_peekable_ref_sum,
315
+ bench_peekable_ext_sum,
292
316
( 0i64 ..1000000 ) . peekable( )
293
317
}
294
318
295
319
bench_sums ! {
296
320
bench_peekable_chain_sum,
297
321
bench_peekable_chain_ref_sum,
322
+ bench_peekable_chain_ext_sum,
298
323
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . peekable( )
299
324
}
300
325
301
326
bench_sums ! {
302
327
bench_skip_sum,
303
328
bench_skip_ref_sum,
329
+ bench_skip_ext_sum,
304
330
( 0i64 ..1000000 ) . skip( 1000 )
305
331
}
306
332
307
333
bench_sums ! {
308
334
bench_skip_chain_sum,
309
335
bench_skip_chain_ref_sum,
336
+ bench_skip_chain_ext_sum,
310
337
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . skip( 1000 )
311
338
}
312
339
313
340
bench_sums ! {
314
341
bench_skip_while_sum,
315
342
bench_skip_while_ref_sum,
343
+ bench_skip_while_ext_sum,
316
344
( 0i64 ..1000000 ) . skip_while( |& x| x < 1000 )
317
345
}
318
346
319
347
bench_sums ! {
320
348
bench_skip_while_chain_sum,
321
349
bench_skip_while_chain_ref_sum,
350
+ bench_skip_while_chain_ext_sum,
322
351
( 0i64 ..1000000 ) . chain( 0 ..1000000 ) . skip_while( |& x| x < 1000 )
323
352
}
324
353
325
354
bench_sums ! {
326
355
bench_take_while_chain_sum,
327
356
bench_take_while_chain_ref_sum,
357
+ bench_take_while_chain_ext_sum,
328
358
( 0i64 ..1000000 ) . chain( 1000000 ..) . take_while( |& x| x < 1111111 )
329
359
}
330
360
331
361
bench_sums ! {
332
362
bench_cycle_take_sum,
333
363
bench_cycle_take_ref_sum,
364
+ bench_cycle_take_ext_sum,
334
365
( 0 ..10000 ) . cycle( ) . take( 1000000 )
335
366
}
336
367
337
368
bench_sums ! {
338
369
bench_cycle_skip_take_sum,
339
370
bench_cycle_skip_take_ref_sum,
371
+ bench_cycle_skip_take_ext_sum,
340
372
( 0 ..100000 ) . cycle( ) . skip( 1000000 ) . take( 1000000 )
341
373
}
342
374
343
375
bench_sums ! {
344
376
bench_cycle_take_skip_sum,
345
377
bench_cycle_take_skip_ref_sum,
378
+ bench_cycle_take_skip_ext_sum,
346
379
( 0 ..100000 ) . cycle( ) . take( 1000000 ) . skip( 100000 )
347
380
}
348
381
349
382
bench_sums ! {
350
383
bench_skip_cycle_skip_zip_add_sum,
351
384
bench_skip_cycle_skip_zip_add_ref_sum,
385
+ bench_skip_cycle_skip_zip_add_ext_sum,
352
386
( 0 ..100000 ) . skip( 100 ) . cycle( ) . skip( 100 )
353
387
. zip( ( 0 ..100000 ) . cycle( ) . skip( 10 ) )
354
388
. map( |( a, b) | a+b)
@@ -359,6 +393,7 @@ bench_sums! {
359
393
bench_sums ! {
360
394
bench_slice_chain_sum,
361
395
bench_slice_chain_ref_sum,
396
+ bench_slice_chain_ext_sum,
362
397
( & [ 0 ; 512 ] ) . iter( ) . chain( ( & [ 1 ; 512 ] ) . iter( ) )
363
398
}
364
399
0 commit comments