Skip to content

Commit

Permalink
add benchmark for extend methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vxpm committed Dec 2, 2023
1 parent d9f82e7 commit af0a5a3
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions benches/slice_and_iter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use segvec::*;

Expand All @@ -12,8 +14,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
//const N: i32 = 10000;
const N: usize = 10000;

let mut group = c.benchmark_group("slice");

let mut group = c.benchmark_group("iterator & slice");
group.bench_function("full Vec iteration", |b| {
let mut v: Vec<usize> = Vec::new();
let mut r = 0xf00ba;
Expand Down Expand Up @@ -80,6 +81,69 @@ pub fn criterion_benchmark(c: &mut Criterion) {
}
});
});

drop(group);
let mut group = c.benchmark_group("extend");
group.measurement_time(Duration::from_secs(20));

const EXTEND_LEN: usize = 8192;
group.bench_function("segvec extend", |b| {
let mut elements = [0; EXTEND_LEN];
let mut state = 0;
for x in elements.iter_mut() {
*x = fast_prng(&mut state);
}

b.iter_with_setup(
|| SegVec::<usize>::new(),
|mut v| {
v.extend(black_box(&elements));
},
);
});
group.bench_function("segvec extend from slice", |b| {
let mut elements = [0; EXTEND_LEN];
let mut state = 0;
for x in elements.iter_mut() {
*x = fast_prng(&mut state);
}

b.iter_with_setup(
|| SegVec::<usize>::new(),
|mut v| {
v.extend_from_slice(black_box(&elements));
},
);
});

group.bench_function("vec extend", |b| {
let mut elements = [0; EXTEND_LEN];
let mut state = 0;
for x in elements.iter_mut() {
*x = fast_prng(&mut state);
}

b.iter_with_setup(
|| Vec::<usize>::new(),
|mut v| {
v.extend(black_box(&elements));
},
);
});
group.bench_function("vec extend from slice", |b| {
let mut elements = [0; EXTEND_LEN];
let mut state = 0;
for x in elements.iter_mut() {
*x = fast_prng(&mut state);
}

b.iter_with_setup(
|| Vec::<usize>::new(),
|mut v| {
v.extend_from_slice(black_box(&elements));
},
);
});
}

criterion_group!(slice_and_iter_bench, criterion_benchmark);
Expand Down

0 comments on commit af0a5a3

Please sign in to comment.