#[cfg(feature = "simd")]
mod simd_demo {
use oxicode::simd::{
decode_simd_array, detect_capability, encode_simd_array, is_simd_available,
};
use oxicode::Error;
use std::hint::black_box;
use std::time::{Duration, Instant};
fn encode_f64_scalar(data: &[f64]) -> Vec<u8> {
let mut out = Vec::with_capacity(8 + data.len() * 8);
out.extend_from_slice(&(data.len() as u64).to_le_bytes());
for value in data {
out.extend_from_slice(&value.to_le_bytes());
}
out
}
fn time_it<T>(
iterations: u32,
mut f: impl FnMut() -> Result<T, Error>,
) -> Result<(Duration, T), Error> {
let warm = f()?;
let start = Instant::now();
let mut last = warm;
for _ in 0..iterations {
last = black_box(f()?);
}
Ok((start.elapsed() / iterations, last))
}
pub fn run() -> Result<(), Error> {
println!("OxiCode SIMD Array Codec Example\n");
println!("detected CPU capability: {:?}", detect_capability());
println!("is_simd_available(): {}\n", is_simd_available());
println!("1. Round-trip via oxicode::simd (its own framing, not encode_to_vec):");
let data: Vec<f64> = (0..10_000).map(|i| i as f64 * 0.5).collect();
let encoded = encode_simd_array(&data)?;
let decoded: Vec<f64> = decode_simd_array(&encoded)?;
assert_eq!(data, decoded);
println!(
" {} f64 values -> {} bytes (8-byte count header + 8 bytes/element), round-trip verified\n",
data.len(),
encoded.len()
);
println!("2. Measured throughput: simd bulk-copy path vs a scalar to_le_bytes loop");
println!(" (memory-bandwidth bound; run with --release for a meaningful number)\n");
for size in [10_000usize, 100_000, 1_000_000] {
let data: Vec<f64> = (0..size).map(|i| i as f64).collect();
let iterations = if size >= 1_000_000 { 20 } else { 200 };
let (simd_time, simd_bytes) = time_it(iterations, || encode_simd_array(&data))?;
let (scalar_time, scalar_bytes) = time_it(iterations, || Ok(encode_f64_scalar(&data)))?;
assert_eq!(simd_bytes, scalar_bytes, "framing must match byte-for-byte");
let simd_gbps = (size * 8) as f64 / simd_time.as_secs_f64() / 1e9;
let scalar_gbps = (size * 8) as f64 / scalar_time.as_secs_f64() / 1e9;
println!(
" {:>9} elements: simd {:>7.2?} ({:>5.2} GB/s) scalar {:>7.2?} ({:>5.2} GB/s) ratio {:.2}x",
size,
simd_time,
simd_gbps,
scalar_time,
scalar_gbps,
scalar_time.as_secs_f64() / simd_time.as_secs_f64().max(f64::EPSILON),
);
}
println!(
"\n Ratios vary by CPU, allocator, and build profile — treat the numbers above as a\n \
local measurement, not a portability guarantee. This crate does not ship a fixed\n \
\"2-4x\" (or any other) speedup claim; measure on your own target hardware."
);
Ok(())
}
}
#[cfg(feature = "simd")]
fn main() -> Result<(), oxicode::Error> {
simd_demo::run()
}
#[cfg(not(feature = "simd"))]
fn main() {
println!("This example requires the \"simd\" feature.");
println!("Run with: cargo run --release --example simd_arrays --features simd");
}