use core::panic; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use project2::{AVLTree, RBTree}; use std::{cmp::Ordering, u32}; pub fn test_AVLTree(tree_size: u32) { // create empty tree let mut tree = AVLTree::AVLTree::new(); // insert all values in list into the tree let list: Vec = (0..tree_size).collect(); for val in list { tree.insert(val); } // search for tree for val in 0..tree_size / 10 { tree.search(val); } } pub fn test_RBTree(tree_size: u32) { // create empty tree let mut tree = RBTree::RBTree::new(); // insert all values in list into the tree let list: Vec = (0..tree_size).collect(); for val in list { tree.insert(val); } // search for tree for val in 0..tree_size / 10 { let success = tree.search(val); if success.is_err() { panic!("node not found???"); } } } fn criterion_benchmark(c: &mut Criterion) { for tree_size in [10000, 40000, 70000, 100000, 130000].iter() { // create vector length tree_size c.bench_function("avl tree", |b| { b.iter(|| test_AVLTree(black_box(*tree_size as u32))) }); c.bench_function("rb tree", |b| { b.iter(|| test_AVLTree(black_box(*tree_size as u32))) }); } } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);