Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawing a circle, but cost over 1 second, it's normal? #324

Closed
yangwenmai opened this issue Jun 13, 2019 · 3 comments
Closed

Drawing a circle, but cost over 1 second, it's normal? #324

yangwenmai opened this issue Jun 13, 2019 · 3 comments

Comments

@yangwenmai
Copy link

yangwenmai commented Jun 13, 2019

//! An example using the drawing functions. Writes to the user-provided target file.

use std::env;
use std::path::Path;
use std::fmt;
use std::time::{Duration, Instant};
use image::{Rgb, RgbImage};
use imageproc::rect::Rect;
use imageproc::drawing::{
    draw_cross_mut,
    draw_line_segment_mut,
    draw_hollow_rect_mut,
    draw_filled_rect_mut,
    draw_hollow_circle_mut,
    draw_filled_circle_mut
};
struct Elapsed(Duration);
impl Elapsed {
    fn from(start: &Instant) -> Self {
        Elapsed(start.elapsed())
    }
}

impl fmt::Display for Elapsed {
    fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match (self.0.as_secs(), self.0.subsec_nanos()) {
            (0, n) if n < 1000 => write!(out, "{} ns", n),
            (0, n) if n < 1000_000 => write!(out, "{} µs", n / 1000),
            (0, n) => write!(out, "{} ms", n / 1000_000),
            (s, n) if s < 10 => write!(out, "{}.{:02} s", s, n / 10_000_000),
            (s, _) => write!(out, "{} s", s),
        }
    }
}

fn main() {
    let arg = if env::args().count() == 2 {
            env::args().nth(1).unwrap()
        } else {
            panic!("Please enter a target file path")
        };
    let timer = Instant::now();
    let path = Path::new(&arg);
    let white = Rgb([255u8, 255u8, 255u8]);

    let mut image = RgbImage::new(1000, 1000);
    // Draw a filled circle within bounds
    draw_filled_circle_mut(&mut image, (500, 500), 400, white);
    image.save(path).unwrap();
    println!("draw in {}", Elapsed::from(&timer));
}

Debug:
Output: draw in 1.22s

Release:
Output:draw in 20ms

env:

MacBookPro 2017
3.1 GHz Intel Core i7
16 GB 2133 MHz LPDDR3

rustc 1.36.0-nightly (33fe1131c 2019-04-20)
cargo 1.36.0-nightly (b6581d383 2019-04-16)

@theotherphil
Copy link
Contributor

theotherphil commented Jun 15, 2019

cargo run --release --example drawing circle.png
draw in 19 ms

cargo run --example drawing circle.png
draw in 1.59 s

What you're reporting matches what I see in debug mode. Do you care about debug mode performance specifically?

@theotherphil
Copy link
Contributor

Closing as answered: this performance from debug builds is not surprising.

@yangwenmai
Copy link
Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants