extern crate criterion; extern crate rblist; use criterion::{black_box, Criterion, criterion_group, criterion_main}; use rblist::{BList, Scale}; const LOOP:i32 = 100; fn benchmark_push_back(c: &mut Criterion) { c.bench_function("push_back {100} elements", |b| { b.iter(|| { let mut list: BList = BList::new(Scale::Large); for i in 0..LOOP { list.push_back(black_box(i)).unwrap(); } }) }); } fn benchmark_insert_before(c: &mut Criterion) { c.bench_function("insert_before at middle", |b| { b.iter(|| { let mut list: BList = BList::new(Scale::Large); for i in 0..LOOP { list.push_back(i).unwrap(); } let node = list.push_back(100).unwrap(); list.insert_before(50, &node).unwrap(); }) }); } fn benchmark_remove(c: &mut Criterion) { c.bench_function("remove element from middle", |b| { b.iter(|| { let mut list: BList = BList::new(Scale::Large); let _ = list.push_back(1).unwrap(); let node2 = list.push_back(2).unwrap(); list.push_back(3).unwrap(); list.remove(&node2).unwrap(); }) }); } fn benchmark_iteration(c: &mut Criterion) { c.bench_function("iterate over {100} elements", |b| { b.iter(|| { let mut list: BList = BList::new(Scale::Large); for i in 0..LOOP { list.push_back(i).unwrap(); } for node in list.iter() { black_box(node.value().unwrap()); } }) }); } use std::collections::LinkedList; fn benchmark_linkedlist_push_back(c: &mut Criterion) { c.bench_function("LinkedList push_back {100} elements", |b| { b.iter(|| { let mut list: LinkedList = LinkedList::new(); for i in 0..LOOP { list.push_back(black_box(i)); } }) }); } fn benchmark_linkedlist_remove(c: &mut Criterion) { c.bench_function("LinkedList remove element from middle", |b| { b.iter(|| { let mut list: LinkedList = LinkedList::new(); let _node1 = list.push_back(1); list.push_back(2); list.push_back(3); list.pop_back(); // Simulate removing element from middle }) }); } fn benchmark_linkedlist_iteration(c: &mut Criterion) { c.bench_function("LinkedList iterate over {100} elements", |b| { b.iter(|| { let mut list: LinkedList = LinkedList::new(); for i in 0..LOOP { list.push_back(i); } for node in list.iter() { black_box(node); } }) }); } criterion_group!(benches, benchmark_push_back, benchmark_insert_before, benchmark_remove, benchmark_iteration, benchmark_linkedlist_push_back, benchmark_linkedlist_remove, benchmark_linkedlist_iteration); criterion_main!(benches);