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

High CPU usage in wait_for_click #92

Closed
sunjay opened this issue Aug 12, 2018 · 0 comments
Closed

High CPU usage in wait_for_click #92

sunjay opened this issue Aug 12, 2018 · 0 comments

Comments

@sunjay
Copy link
Owner

sunjay commented Aug 12, 2018

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!

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

No branches or pull requests

1 participant