Closed
Description
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.
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:
- Verify that you see high CPU usage when following the instructions above
- Add a call to either
thread::sleep
orself.wait
to the end of the body of the loop inwait_for_click
- The number of milliseconds to wait should probably be about
1000.0 / 60.0
or1000.0 / 120.0
since those are the number of milliseconds in a frame at 60 FPS and 120 FPS
- The number of milliseconds to wait should probably be about
- 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!