use std::{collections::BTreeMap, time::Instant};
use fp_columnar::Column;
use fp_frame::DataFrame;
use fp_index::Index;
fn sm(i: usize, s: u64) -> u64 {
let mut h = (i as u64).wrapping_add(s).wrapping_mul(0x9E3779B97F4A7C15);
h = (h ^ (h >> 30)).wrapping_mul(0xBF58476D1CE4E5B9);
h = (h ^ (h >> 27)).wrapping_mul(0x94D049BB133111EB);
h ^= h >> 31;
h
}
fn main() {
let a: Vec<String> = std::env::args().collect();
let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
let op = a.get(2).map(String::as_str).unwrap_or("drop_dup");
let it: usize = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(6);
let index = Index::new_known_unique_int64_unit_range(0, n);
let mut cols = BTreeMap::new();
cols.insert(
"a".to_string(),
Column::from_i64_values((0..n).map(|i| (sm(i, 0) % 100000) as i64).collect()),
);
cols.insert(
"b".to_string(),
Column::from_i64_values((0..n).map(|i| (sm(i, 1) % 500) as i64).collect()),
);
cols.insert(
"c".to_string(),
Column::from_f64_values((0..n).map(|i| sm(i, 2) as f64).collect()),
);
let df =
DataFrame::new_with_column_order(index, cols, vec!["a".into(), "b".into(), "c".into()])
.unwrap();
let mut best = u128::MAX;
for _ in 0..it {
let t = Instant::now();
match op {
"sort2" => {
std::hint::black_box(
df.sort_values_multi(&["a", "b"], &[true, true], "last")
.unwrap(),
);
}
"nunique" => {
std::hint::black_box(df.nunique().unwrap());
}
"count" => {
std::hint::black_box(df.count().unwrap());
}
_ => panic!(),
};
let e = t.elapsed().as_nanos();
if e < best {
best = e;
}
}
println!("{op} n={n}: best={best}ns");
}