diff --git a/examples/slab.rs b/examples/slab.rs index 28abad2..90d8aa6 100644 --- a/examples/slab.rs +++ b/examples/slab.rs @@ -3,28 +3,29 @@ use std::{mem, ptr, time::Instant}; use algorithm::{Reinit, Slab}; -const ARRAY_SIZE: usize = 10240; -const NUM: usize = usize::MAX; +const ARRAY_SIZE: usize = 102400; +const NUM: usize = usize::MAX - 99999; const ZERO_ARRAY: [usize; ARRAY_SIZE] = [NUM; ARRAY_SIZE]; struct TestStruct { array: [usize; ARRAY_SIZE], size: usize, + val: String, } impl Default for TestStruct { fn default() -> Self { - Self { array: [NUM; ARRAY_SIZE], size: 0 } + Self { array: [NUM; ARRAY_SIZE], size: 0, val: "slab".to_string(), } } } impl Reinit for TestStruct { #[inline(always)] fn reinit(&mut self) { - // self.array.fill(0); + self.size = 0; + self.val.clear(); + self.val.push_str("slab"); unsafe { - // ptr::write_bytes(&mut self.array[0], u8::MAX, ARRAY_SIZE); ptr::copy_nonoverlapping(&ZERO_ARRAY[0], &mut self.array[0], ARRAY_SIZE); - // libc::memset(&mut self.array[0] as *mut usize as *mut libc::c_void, 0, ARRAY_SIZE * mem::size_of::()); } } } @@ -38,7 +39,7 @@ fn test_speed() { for i in 0..times { let (next, test) = slab.get_reinit_next_val(); test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); - sum = sum.wrapping_add(test.array[10] + test.size); + sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); slab.remove(next); } println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum); @@ -51,7 +52,7 @@ fn test_speed() { let next = slab.insert(TestStruct::default()); let test = &mut slab[next]; test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); - sum = sum.wrapping_add(test.array[10] + test.size); + sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); slab.remove(next); } println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum); @@ -61,7 +62,7 @@ fn test_speed() { for i in 0..times { let mut test = TestStruct::default(); test.array[i % 20] = test.array[i % 20].wrapping_add(i % 1024); - sum = sum.wrapping_add(test.array[10] + test.size); + sum = sum.wrapping_add(test.array[10] + test.size + test.val.len()); drop(test); } println!("all cost times {}, sum = {}", now.elapsed().as_nanos(), sum);