sunjay / turtle

Create Animated Drawings in Rust
http://turtle.rs
Mozilla Public License 2.0
562 stars 53 forks source link

High CPU usage in wait_for_click #92

Closed sunjay closed 6 years ago

sunjay commented 6 years ago

Run this example code:

extern crate turtle;

use turtle::Turtle;

fn main() {
    let mut turtle = Turtle::new();

    turtle.wait_for_click();
    turtle.forward(300.0);
}

Let the process run for a while without clicking or doing anything. You will notice that the process begins to use a lot of CPU power.

image

This is likely because wait_for_click is looping continuously waiting for a click event. We should probably sleep in that loop for some small amount of time to avoid excessively using CPU.

To fix this bug:

  1. Verify that you see high CPU usage when following the instructions above
  2. Add a call to either thread::sleep or self.wait to the end of the body of the loop in wait_for_click
    • The number of milliseconds to wait should probably be about 1000.0 / 60.0 or 1000.0 / 120.0 since those are the number of milliseconds in a frame at 60 FPS and 120 FPS
  3. Verify that the CPU usage is no longer high when following the instructions above

If any of these instructions do not work or if you need more help, please let me know! I am happy to help!