criterion + flamegraph
****Vec > LinkedList CPU 64B#[repr(C)]
Vec::with_capacity(n)reallocString::with_capacity(n)smallvec/tinyvecbytescrate buffer
****#[inline] / #[inline(always)] Profile-Guided OptimizationPGOBOLT
**** get_unchecked()unsafeunsafe invariant
// realloc
let mut v = Vec::with_capacity(1000);
for i in 0..1000 { v.push(i); }
//
use std::borrow::Cow;
fn process(s: Cow<str>) -> Cow<str> {
if s.len() > 100 { Cow::Owned(s[..100].to_string()) }
else { s }
}
//
#[inline]
fn hot_path(x: i32) -> i32 { x * 2 }
// criterion
use criterion::{criterion_group, criterion_main, Criterion};
fn bench_add(c: &mut Criterion) {
c.bench_function("add", |b| b.iter(|| add(2, 3)));
}
criterion_group!(benches, bench_add);
criterion_main!(benches);cargo bench | |
cargo flamegraph | CPU |
cargo bloat | |
perf record | Linux |
heaptrack | |
cachegrind |