This commit is contained in:
Florian Stecker 2024-02-23 10:02:54 -05:00
parent feadef4723
commit fa3f3006f3
2 changed files with 27 additions and 19 deletions

View File

@ -5,6 +5,7 @@ fn main() {
// generate 800MB of random data
let mut data: Vec<u64> = 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<(u128, u128, u128)>> = Vec::new();
@ -35,17 +36,17 @@ fn main() {
}
fn test_searches(data: &[u64], runs: usize, items: usize) -> Vec<Duration> {
let needle_offset = random::<u64>();
// let offset_offset = random::<usize>();
let offset_offset = items;
let needle_offset = random::<usize>();
let offset_offset = random::<usize>();
// 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<Duration> {
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<Duration> {
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<Duration> {
vec![linear_iter_duration, linear_loop_duration, binary_duration]
}
#[allow(dead_code)]
fn flush_caches() {
let mut data: Vec<u64> = 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;
}

View File

@ -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 "<paste data_d7 data_d8" \
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", \
"" using 2:($8/$1/4) w l lw 2 t "linear, iterator, spread", \
"" using 2:($9/$1/4) w l lw 2 t "linear, loop, spread", \
"" using 2:($10/$1/4) w l lw 2 t "binary, spread"
# plot "<paste data_d7 data_d8" \
# using 2:(($8-$3)/$1/4) w l lw 2 t "linear, iterator", \
# "" using 2:(($9-$4)/$1/4) w l lw 2 t "linear, loop", \
# "" using 2:(($10-$5)/$1/4) w l lw 2 t "binary"