|
| 1 | +use rust_data_structures::skip_list::SkipList; |
| 2 | +use std::collections::BTreeSet; |
| 3 | +use std::hint::black_box; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +const SIZE: i32 = 20_000; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + println!("skip list benchmark (manual, std::time::Instant)"); |
| 10 | + println!("size: {SIZE}"); |
| 11 | + println!("ordered insert: {:?}", bench_ordered_insert()); |
| 12 | + println!("permuted insert: {:?}", bench_permuted_insert()); |
| 13 | + println!("search: {:?}", bench_search()); |
| 14 | + println!("iteration: {:?}", bench_iteration()); |
| 15 | + println!("std btreeset search: {:?}", bench_btreeset_search()); |
| 16 | +} |
| 17 | + |
| 18 | +fn bench_ordered_insert() -> Duration { |
| 19 | + let start = Instant::now(); |
| 20 | + let mut list = SkipList::with_seed(20, 0.5, 1).unwrap(); |
| 21 | + for value in 0..SIZE { |
| 22 | + black_box(list.insert(value)); |
| 23 | + } |
| 24 | + start.elapsed() |
| 25 | +} |
| 26 | + |
| 27 | +fn bench_permuted_insert() -> Duration { |
| 28 | + let start = Instant::now(); |
| 29 | + let mut list = SkipList::with_seed(20, 0.5, 1).unwrap(); |
| 30 | + for value in permuted_values() { |
| 31 | + black_box(list.insert(value)); |
| 32 | + } |
| 33 | + start.elapsed() |
| 34 | +} |
| 35 | + |
| 36 | +fn bench_search() -> Duration { |
| 37 | + let mut list = SkipList::with_seed(20, 0.5, 1).unwrap(); |
| 38 | + for value in permuted_values() { |
| 39 | + list.insert(value); |
| 40 | + } |
| 41 | + |
| 42 | + let start = Instant::now(); |
| 43 | + for value in 0..SIZE { |
| 44 | + black_box(list.contains(&value)); |
| 45 | + } |
| 46 | + start.elapsed() |
| 47 | +} |
| 48 | + |
| 49 | +fn bench_iteration() -> Duration { |
| 50 | + let mut list = SkipList::with_seed(20, 0.5, 1).unwrap(); |
| 51 | + for value in permuted_values() { |
| 52 | + list.insert(value); |
| 53 | + } |
| 54 | + |
| 55 | + let start = Instant::now(); |
| 56 | + for value in list.iter() { |
| 57 | + black_box(value); |
| 58 | + } |
| 59 | + start.elapsed() |
| 60 | +} |
| 61 | + |
| 62 | +fn bench_btreeset_search() -> Duration { |
| 63 | + let set = permuted_values().into_iter().collect::<BTreeSet<_>>(); |
| 64 | + |
| 65 | + let start = Instant::now(); |
| 66 | + for value in 0..SIZE { |
| 67 | + black_box(set.contains(&value)); |
| 68 | + } |
| 69 | + start.elapsed() |
| 70 | +} |
| 71 | + |
| 72 | +fn permuted_values() -> Vec<i32> { |
| 73 | + (0..SIZE).map(|value| (value * 37) % SIZE).collect() |
| 74 | +} |
0 commit comments