Skip to main content

bench_sift/
bench_sift.rs

1/*
2 * bench_sift.rs
3 *
4 * Copyright (c) 2026 ParkSangWoo
5 *
6 * Author(s):
7 *
8 *      ParkSangWoo <dev.parksangwoo@gmail.com>
9 *
10 *
11 * This file is part of cumulus3d.
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 *      http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * SPDX-License-Identifier: Apache-2.0
26 */
27
28//! SIFT timing: `cargo run --release -p cumulus3d-features --example bench_sift -- <image|synthetic> [W H]`
29//! If W H is given, the image is resampled to that size before measuring.
30
31use cumulus3d_features::*;
32use std::time::Instant;
33
34fn main() {
35    let args: Vec<String> = std::env::args().collect();
36    let src = args.get(1).cloned().unwrap_or_else(|| "synthetic".into());
37    let size: Option<(usize, usize)> = match (args.get(2), args.get(3)) {
38        (Some(w), Some(h)) => Some((w.parse().expect("W"), h.parse().expect("H"))),
39        _ => None,
40    };
41    let t = Instant::now();
42    let mut g = if src == "synthetic" {
43        let (w, h) = size.unwrap_or((2048, 1152));
44        let mut s = 1u64;
45        let mut noise = vec![0f32; w * h];
46        for v in noise.iter_mut() {
47            s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
48            *v = (s >> 40) as f32 / (1u64 << 24) as f32;
49        }
50        let mut out = vec![0f32; w * h];
51        let mut tmp = vec![0f32; w * h];
52        sift::pyramid::gaussian_blur(&noise, &mut out, &mut tmp, w, h, 2.0);
53        GrayImage::from_f32(w, h, |x, y| (out[y * w + x] - 0.5) * 3.0 + 0.5)
54    } else {
55        read_gray(std::path::Path::new(&src)).expect("영상 읽기")
56    };
57    let t_read = t.elapsed();
58    if let Some((w, h)) = size {
59        if (w, h) != (g.width, g.height) {
60            g = g.resized(w, h);
61        }
62    }
63    println!("입력 {} ({}x{}), 읽기 {:.1} ms, 스레드 {}", src, g.width, g.height, t_read.as_secs_f64() * 1e3, rayon::current_num_threads());
64    let sift = CpuSift::new();
65    for (name, opts) in [("compat(기본)", SiftOptions::default()), ("무제한", SiftOptions { max_num_features: 0, ..Default::default() })]
66    {
67        let mut times = Vec::new();
68        let mut n = 0;
69        for _ in 0..5 {
70            let t = Instant::now();
71            let out = sift.extract(&g, &opts).expect("추출");
72            times.push(t.elapsed().as_secs_f64() * 1e3);
73            n = out.len();
74        }
75        let first = times[0];
76        let mut rest = times[1..].to_vec();
77        rest.sort_by(|a, b| a.total_cmp(b));
78        println!("{name}: 특징 {n}, 첫 실행 {first:.1} ms, 이후 중앙값 {:.1} ms", rest[rest.len() / 2]);
79    }
80}