From af0a5a3995f75e4b87ccaf99d542fda215c2e131 Mon Sep 17 00:00:00 2001 From: vxpm Date: Sat, 2 Dec 2023 00:49:25 -0300 Subject: [PATCH] add benchmark for extend methods --- benches/slice_and_iter.rs | 68 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/benches/slice_and_iter.rs b/benches/slice_and_iter.rs index 929e02a..1a341bb 100644 --- a/benches/slice_and_iter.rs +++ b/benches/slice_and_iter.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use criterion::{black_box, criterion_group, criterion_main, Criterion}; use segvec::*; @@ -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 = Vec::new(); let mut r = 0xf00ba; @@ -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::::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::::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::::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::::new(), + |mut v| { + v.extend_from_slice(black_box(&elements)); + }, + ); + }); } criterion_group!(slice_and_iter_bench, criterion_benchmark);