#![deny(
clippy::all,
clippy::cargo,
clippy::nursery,
// clippy::restriction,
// clippy::pedantic
)]
#![allow(
clippy::suboptimal_flops,
clippy::redundant_pub_crate,
clippy::fallible_impl_from
)]
#![deny(missing_debug_implementations)]
#![deny(rustdoc::all)]
use std::time::Instant;
use spectrum_analyzer::*;
fn main() {
let samples = (0..2048)
.map(|_| rand::random::<i16>())
.map(|x| x as f32)
.collect::<Vec<_>>();
let hann_window = windows::hann_window(&samples);
let bench_res_without_scaling = bench_without_scaling(hann_window.clone());
let bench_res_with_scaling = bench_with_scaling(hann_window);
println!(
"Bench without scaling: avg = {}us per Iteration",
bench_res_without_scaling
);
println!(
"Bench with scaling: avg = {}us per Iteration",
bench_res_with_scaling
);
}
fn bench_without_scaling(samples: Vec<f32>) -> u64 {
let fnc = move || samples_fft_to_spectrum(&samples, 44100, FrequencyLimit::All, None).unwrap();
bench_fnc(Box::new(fnc))
}
fn bench_with_scaling(samples: Vec<f32>) -> u64 {
let fnc = move || {
samples_fft_to_spectrum(
&samples,
44100,
FrequencyLimit::All,
Some(&scaling::divide_by_N),
)
.unwrap()
};
bench_fnc(Box::new(fnc))
}
fn bench_fnc(fnc: Box<dyn Fn() -> FrequencySpectrum>) -> u64 {
for _ in 0..10 {
let _ = fnc();
}
let now = Instant::now();
let runs = 10000;
for _ in 0..runs {
let _ = fnc();
}
let duration = now.elapsed();
(duration.as_micros() / runs) as u64
}