Skip to main content

bench_to_csv_mixed/
bench_to_csv_mixed.rs

1//! to_csv over a mixed {Int64, Float64, Datetime64} frame. bench_to_csv_mixed <n>
2use std::collections::BTreeMap;
3
4use fp_columnar::Column;
5use fp_frame::DataFrame;
6use fp_index::Index;
7
8fn main() {
9    let a: Vec<String> = std::env::args().collect();
10    let n: usize = a.get(1).and_then(|s| s.parse().ok()).unwrap_or(1_000_000);
11    let ints: Vec<i64> = (0..n as i64)
12        .map(|i| (i.wrapping_mul(2_654_435_761)) % 1_000_000)
13        .collect();
14    let floats: Vec<f64> = (0..n).map(|i| (i as f64) * 0.5 - 100.0).collect();
15    let base = 946_684_800_000_000_000i64;
16    let step = 37_000_000_000i64;
17    let dts: Vec<i64> = (0..n as i64).map(|i| base + i * step).collect();
18    let mut cols: BTreeMap<String, Column> = BTreeMap::new();
19    cols.insert("a".to_string(), Column::from_i64_values(ints));
20    cols.insert("b".to_string(), Column::from_f64_values(floats));
21    cols.insert("t".to_string(), Column::from_datetime64_values(dts));
22    let frame = DataFrame::new(Index::new_known_unique_int64_unit_range(0, n), cols).unwrap();
23    let mut best = u128::MAX;
24    for _ in 0..6 {
25        let t = std::time::Instant::now();
26        let s = fp_io::write_csv_string(&frame).unwrap();
27        std::hint::black_box(s.len());
28        let e = t.elapsed().as_nanos();
29        if e < best {
30            best = e;
31        }
32    }
33    println!(
34        "to_csv mixed+dt n={n}: best={best}ns ({:.2}ms)",
35        best as f64 / 1e6
36    );
37}