2017-02-06 10:38:47 +00:00
|
|
|
use std::collections::VecDeque;
|
2019-11-30 03:30:49 +00:00
|
|
|
use test::{black_box, Bencher};
|
2017-02-06 10:38:47 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_new(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let ring: VecDeque<i32> = VecDeque::new();
|
|
|
|
black_box(ring);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_grow_1025(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let mut deq = VecDeque::new();
|
|
|
|
for i in 0..1025 {
|
|
|
|
deq.push_front(i);
|
|
|
|
}
|
|
|
|
black_box(deq);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_1000(b: &mut Bencher) {
|
|
|
|
let ring: VecDeque<_> = (0..1000).collect();
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let mut sum = 0;
|
|
|
|
for &i in &ring {
|
|
|
|
sum += i;
|
|
|
|
}
|
|
|
|
black_box(sum);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mut_iter_1000(b: &mut Bencher) {
|
|
|
|
let mut ring: VecDeque<_> = (0..1000).collect();
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let mut sum = 0;
|
|
|
|
for i in &mut ring {
|
|
|
|
sum += *i;
|
|
|
|
}
|
|
|
|
black_box(sum);
|
|
|
|
})
|
|
|
|
}
|
2019-02-02 00:29:39 +00:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_try_fold(b: &mut Bencher) {
|
|
|
|
let ring: VecDeque<_> = (0..1000).collect();
|
|
|
|
|
|
|
|
b.iter(|| black_box(ring.iter().try_fold(0, |a, b| Some(a + b))))
|
|
|
|
}
|