use criterion::{
BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main,
};
use roto::{List, Runtime};
pub fn bench_insertion_sort(c: &mut Criterion) {
let rt = Runtime::new();
let mut pkg = rt.compile("benches/insertion_sort.roto").unwrap();
let fib = pkg
.get_function::<fn(List<u64>) -> List<u64>>("main")
.unwrap();
let mut group = c.benchmark_group("insertion_sort");
for (name, n) in [
(
"1 to 10 scrambled",
List::from([1, 2, 9, 10, 3, 4, 7, 8, 5, 6]),
),
("0 to 100", (0..100).collect()),
("100 to 0", (0..100).rev().collect()),
] {
{
let x = n.clone().into_iter().collect();
let y = fib.call(x);
let mut prev = 0;
for x in y {
if x >= prev {
prev = x;
} else {
panic!()
}
}
}
group.bench_with_input(
BenchmarkId::from_parameter(name),
&n,
|b, n| {
b.iter_batched(
|| n.clone().into_iter().collect::<List<_>>(),
|n| fib.call(std::hint::black_box(n)),
BatchSize::SmallInput,
)
},
);
}
}
criterion_group!(benches, bench_insertion_sort);
criterion_main!(benches);