Skip to main content

bench/
bench.rs

1//! `cargo run --release --example bench`: rough timings, single-threaded vs automatic threads.
2use log_fft::{ChirpZ, Dft, Fp, InverseChirpZ, Ntt};
3use std::time::Instant;
4
5fn time<F: FnMut()>(mut f: F) -> f64 {
6    f(); // warm up
7    let reps = 5;
8    let t = Instant::now();
9    for _ in 0..reps {
10        f();
11    }
12    t.elapsed().as_secs_f64() / reps as f64 * 1e3
13}
14
15fn main() {
16    println!("{:<26} {:>10} {:>10} {:>10}", "transform", "n", "1 thread", "auto");
17    for log in [16, 20, 22] {
18        let n = 1usize << log;
19        let mut x: Vec<Fp> = (0..n as u64).map(Fp::new).collect();
20        let (p1, pa) = (Ntt::new(n).with_threads(1), Ntt::new(n));
21        let a = time(|| p1.forward(&mut x));
22        let b = time(|| pa.forward(&mut x));
23        println!("{:<26} {:>10} {:>10.2} {:>10.2}", "Ntt", n, a, b);
24    }
25    for n in [65535usize, 255 * 4096, 65537 * 15] {
26        let x: Vec<Fp> = (0..n as u64).map(Fp::new).collect();
27        let (p1, pa) = (Dft::new(n).with_threads(1), Dft::new(n));
28        let a = time(|| { p1.forward(&x); });
29        let b = time(|| { pa.forward(&x); });
30        println!("{:<26} {:>10} {:>10.2} {:>10.2}", format!("Dft {:?}", pa.factors()), n, a, b);
31    }
32    for n in [100_000usize, 1_000_000] {
33        let q = Fp::new(3);
34        let x: Vec<Fp> = (0..n as u64).map(Fp::new).collect();
35        let c = ChirpZ::new(n, n, q);
36        println!("{:<26} {:>10} {:>10} {:>10.2}", "ChirpZ (q=3)", n, "-", time(|| { c.apply(&x); }));
37        let ic = InverseChirpZ::new(n, q);
38        println!("{:<26} {:>10} {:>10} {:>10.2}", "InverseChirpZ (q=3)", n, "-", time(|| { ic.apply(&x); }));
39    }
40}