use std::any::type_name; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use importunate::{inner::Inner, *}; pub fn criterion_benchmark(c: &mut Criterion) { bench_new_index::(c, 0); bench_new_index::(c, 33); bench_old_index::(c, 0); bench_old_index::(c, 33); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_worst_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_combine_medium_case::(c); bench_calculate::(c); bench_calculate::(c); bench_calculate::(c); bench_calculate::(c); bench_calculate::(c); bench_calculate::(c); bench_calculate::(c); bench_apply::(c); bench_apply::(c); bench_apply::(c); bench_apply::(c); bench_apply::(c); bench_apply::(c); bench_apply::(c); } fn bench_old_index(c: &mut Criterion, index: u8) { c.bench_function(format!("old index {SIZE} {index}").as_str(), |b| { let perm = Permutation::::get_last(); b.iter(|| old_index(black_box(perm), index)) }); } fn bench_new_index(c: &mut Criterion, index: u8) { c.bench_function(format!("new index {SIZE} {index}").as_str(), |b| { let perm = Permutation::::get_last(); b.iter(|| new_index(black_box(perm), index)) }); } fn bench_apply(c: &mut Criterion) { c.bench_function(format!("apply {} {SIZE}", type_name::()).as_str(), |b| { let arr = Permutation::::get_last().get_array(); let perm = Permutation::::try_calculate(arr, |&x| x).unwrap(); let test_arr = arr; b.iter(|| apply(black_box(test_arr), perm)) }); } fn bench_calculate(c: &mut Criterion) { c.bench_function( format!("calculate {} {SIZE}", type_name::()).as_str(), |b| { let mut arr: [u8; SIZE] = Permutation::::default().get_array(); arr.reverse(); let test_arr = arr; b.iter(|| calculate::(black_box(test_arr))) }, ); } fn bench_combine_medium_case(c: &mut Criterion) { c.bench_function( format!("combine_medium {} {SIZE}", type_name::()).as_str(), |b| { let lhs = Permutation::::interleave(2); let rhs = Permutation::::interleave(3); b.iter(|| combine::(black_box(lhs), black_box(&rhs))) }, ); } fn bench_combine_worst_case(c: &mut Criterion) { c.bench_function( format!("combine_worst {} {SIZE}", type_name::()).as_str(), |b| { let lhs = Permutation::::get_last(); let rhs = Permutation::::get_last(); b.iter(|| combine::(black_box(lhs), black_box(&rhs))) }, ); } fn calculate(arr: [u8; SIZE]) -> Permutation { Permutation::calculate_unchecked(arr, |&x| x) } fn apply( mut arr: [u8; SIZE], permutation: Permutation, ) -> [u8; SIZE] { permutation.apply(&mut arr); arr } fn new_index(permutation: Permutation, index: u8) -> u8 { permutation.index_of(&index, |&x| x) } fn old_index(permutation: Permutation, index: u8) -> u8 { permutation.element_at_index(index, |x| x) } fn combine( lhs: Permutation, rhs: &Permutation, ) -> Permutation { lhs.combine(rhs) } criterion_group!(benches, criterion_benchmark); criterion_main!(benches);