nucleo-l053r8-benches/src/bench.rs

43 lines
1.1 KiB
Rust

use algorithms::crc::{Crc, Crc32 as Crc32Rust, ffi::Crc32 as Crc32C};
use defmt::info;
use hal::{
delay::Delay,
gpio::{Output, Pin, PushPull},
prelude::*,
};
#[inline]
pub fn bench(pin: &mut Pin<Output<PushPull>>, delay: &mut Delay) {
bench_delay(pin, delay);
bench_crc(pin, delay);
}
fn bench_delay(pin: &mut Pin<Output<PushPull>>, delay: &mut Delay) {
time_exec(pin, || delay.delay_ms(20u32));
time_exec(pin, || delay.delay_ms(50u32));
}
fn time_exec<F>(pin: &mut Pin<Output<PushPull>>, mut f: F)
where
F: FnMut(),
{
info!("Running the requested function 50 times and returning the average");
const RUNS: u32 = 50;
pin.set_high().expect("could not set pun to high");
for _ in 0..RUNS {
f();
}
pin.set_low().expect("could not set pun to low");
}
fn bench_crc(pin: &mut Pin<Output<PushPull>>, delay: &mut Delay) {
const DATA: &[u8] = b"hello world AAAAAAAAAAAAAAAAAAAAAAA";
delay.delay_ms(50_u32);
time_exec(pin, || {
Crc32Rust::checksum(DATA);
});
delay.delay_ms(50_u32);
time_exec(pin, || {
Crc32C::checksum(DATA);
});
}