extern crate nanobox; use nanobox::NanoBox; fn nanobox_small_item_small_space(b: &mut Bencher) { b.iter(|| { let small: NanoBox<_, 1> = black_box(NanoBox::new(black_box(true))); small }) } fn nanobox_small_item_large_space(b: &mut Bencher) { b.iter(|| { let small: NanoBox<_, 64> = black_box(NanoBox::new(black_box(true))); small }) } fn nanobox_large_item_small_space(b: &mut Bencher) { b.iter(|| { let large: NanoBox<_, 1> = black_box(NanoBox::new(black_box([0usize; 64]))); large }) } fn nanobox_large_item_large_space(b: &mut Bencher) { b.iter(|| { let large: NanoBox<_, 64> = black_box(NanoBox::new(black_box([0usize; 64]))); // assert!(!large.is_heap()); large }) } fn box_small_item(b: &mut Bencher) { b.iter(|| { let large: Box<_> = black_box(Box::new(black_box(true))); large }) } fn box_large_item(b: &mut Bencher) { b.iter(|| { let large: Box<_> = black_box(Box::new(black_box([0usize; 64]))); large }) } use std::hint::black_box; use criterion::criterion_group; use criterion::criterion_main; use criterion::Bencher; use criterion::Criterion; fn box_small(c: &mut Criterion) { c.bench_function( "nanobox_small_item_small_space", nanobox_small_item_small_space, ); c.bench_function( "nanobox_small_item_large_space", nanobox_small_item_large_space, ); c.bench_function("box_small_item", box_small_item); } fn box_big(c: &mut Criterion) { c.bench_function( "nanobox_large_item_small_space", nanobox_large_item_small_space, ); c.bench_function( "nanobox_large_item_large_space", nanobox_large_item_large_space, ); c.bench_function("box_large_item", box_large_item); } criterion_group!(benches, box_small, box_big); criterion_main!(benches);