Skip to main content

overhead/
overhead.rs

1//! Compare wrapper paths using a tiny known-result model (not a production-model benchmark).
2use mnn_runtime::{Runtime, RuntimeConfig, Tensor};
3use std::time::{Duration, Instant};
4
5fn measure(mut run: impl FnMut(), iterations: u32) -> (Duration, Duration, Duration) {
6    for _ in 0..100 {
7        run();
8    }
9    let mut times = Vec::new();
10    for _ in 0..iterations {
11        let start = Instant::now();
12        run();
13        times.push(start.elapsed());
14    }
15    times.sort_unstable();
16    (
17        times[times.len() / 2],
18        times[times.len() * 95 / 100],
19        times[times.len() * 99 / 100],
20    )
21}
22
23fn main() -> Result<(), Box<dyn std::error::Error>> {
24    let runtime = Runtime::new(RuntimeConfig::new().with_threads(1))?;
25    let model =
26        runtime.load_bytes(include_bytes!("../tests/fixtures/packed_output3.mnn").as_slice())?;
27    let input = Tensor::new("input", vec![1, 3, 1, 2], vec![1.0; 6])?;
28    let borrowed = measure(
29        || {
30            std::hint::black_box(model.run(std::slice::from_ref(&input)).unwrap());
31        },
32        1000,
33    );
34    let mut outputs = model.run(std::slice::from_ref(&input))?;
35    let reused = measure(
36        || {
37            let result = model
38                .run_reusing(vec![input.clone()], std::mem::take(&mut outputs))
39                .unwrap();
40            outputs = std::hint::black_box(result.tensors);
41        },
42        1000,
43    );
44    println!("tiny model, current compilation profile; P50/P95/P99");
45    println!("borrowed: {borrowed:?}; reused outputs: {reused:?}");
46    println!(
47        "Input cloning is included in both loops; run_owned transfers an existing Vec without that clone."
48    );
49    Ok(())
50}