From fa3f3006f307f076362a94f0c0748c4b7d2c5aa6 Mon Sep 17 00:00:00 2001 From: Florian Stecker Date: Fri, 23 Feb 2024 10:02:54 -0500 Subject: [PATCH] bug fix --- src/main.rs | 26 ++++++++++++++------------ times.plt | 20 +++++++++++++------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 07cb186..d643d94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ fn main() { // generate 800MB of random data let mut data: Vec = vec![0; 100*1024*1024]; thread_rng().fill(&mut data[..]); + data.sort(); let mut metadata: Vec<(usize, usize)> = Vec::new(); let mut all_times: Vec> = Vec::new(); @@ -35,17 +36,17 @@ fn main() { } fn test_searches(data: &[u64], runs: usize, items: usize) -> Vec { - let needle_offset = random::(); - // let offset_offset = random::(); - let offset_offset = items; + let needle_offset = random::(); + let offset_offset = random::(); + // let offset_offset = items; flush_caches(); let start_time = Instant::now(); for i in 0..runs { let offset = usize::wrapping_mul(i+1, offset_offset) % (data.len() - items); - let needle = u64::wrapping_mul((i+1) as u64, needle_offset); - search_linear_iter(&data[offset..offset+items], needle); + let needle_index = usize::wrapping_mul(i+1, needle_offset) % items; + search_linear_iter(&data[offset..offset+items], data[offset+needle_index]); } let linear_iter_duration = Instant::now().duration_since(start_time); @@ -54,8 +55,8 @@ fn test_searches(data: &[u64], runs: usize, items: usize) -> Vec { let start_time = Instant::now(); for i in 0..runs { let offset = usize::wrapping_mul(i+1, offset_offset) % (data.len() - items); - let needle = u64::wrapping_mul((i+1) as u64, needle_offset); - search_linear_loop(&data[offset..offset+items], needle); + let needle_index = usize::wrapping_mul(i+1, needle_offset) % items; + search_linear_loop(&data[offset..offset+items], data[offset+needle_index]); } let linear_loop_duration = Instant::now().duration_since(start_time); @@ -64,8 +65,8 @@ fn test_searches(data: &[u64], runs: usize, items: usize) -> Vec { let start_time = Instant::now(); for i in 0..runs { let offset = usize::wrapping_mul(i+1, offset_offset) % (data.len() - items); - let needle = u64::wrapping_mul((i+1) as u64, needle_offset); - search_binary(&data[offset..offset+items], needle); + let needle_index = usize::wrapping_mul(i+1, needle_offset) % items; + search_binary(&data[offset..offset+items], data[offset+needle_index]); } let binary_duration = Instant::now().duration_since(start_time); @@ -74,6 +75,7 @@ fn test_searches(data: &[u64], runs: usize, items: usize) -> Vec { vec![linear_iter_duration, linear_loop_duration, binary_duration] } +#[allow(dead_code)] fn flush_caches() { let mut data: Vec = vec![0; 1024*1024]; thread_rng().fill(&mut data[..]); @@ -96,9 +98,9 @@ fn search_binary(haystack: &[u64], needle: u64) { fn search_linear_iter(haystack: &[u64], needle: u64) { let index = haystack.iter() .enumerate() - .skip_while(|(_,x)|**x != needle) + .skip_while(|&(_,&x)|x < needle) .next() - .map(|x|x.0) + .map(|(i,_)|i) .unwrap_or_else(||haystack.len()); if index as u64 == needle { @@ -109,7 +111,7 @@ fn search_linear_iter(haystack: &[u64], needle: u64) { fn search_linear_loop(haystack: &[u64], needle: u64) { let mut index = 0; for i in 0..haystack.len() { - if haystack[i] == needle { + if haystack[i] >= needle { index = i; break; } diff --git a/times.plt b/times.plt index edfff2b..481c753 100644 --- a/times.plt +++ b/times.plt @@ -5,11 +5,17 @@ set yrange [1:3000] set grid set terminal pngcairo size 1024, 1024 -set output "times.png" +set output "times_d8.png" -plot "data_c8" using 2:($3/$1/4) w l lw 2 t "linear, iterator, consecutive", \ - "" using 2:($4/$1/4) w l lw 2 t "linear, loop, consecutive", \ - "" using 2:($5/$1/4) w l lw 2 t "binary, consecutive", \ - "data_c7" using 2:($3/$1/4) w l lw 2 t "linear, iterator, spread", \ - "" using 2:($4/$1/4) w l lw 2 t "linear, loop, spread", \ - "" using 2:($5/$1/4) w l lw 2 t "binary, spread" +plot "