Piston lagging when update is too fast?

#1

I’m writing a game with Piston and I have found that it lags. I was digging through the code and decided to edit the update loop like so: (similar to the code shown here)

if let Some(ref u) = e.update_args() {
    let start = std::time::Instant::now();
    app.update(u);
    println!("{:?}", std::time::Instant::now().duration_since(start));
}

And to my surprise adding the println! and the duration calculations actually makes it faster for some weird reason. The output of this setup looks like this

13.521µs
18.629µs
16.525µs
15.023µs
15.323µs
14.122µs
39.962µs
43.868µs
21.333µs

And to time a println! I changed it to this:

if let Some(ref u) = e.update_args() {
    let start = std::time::Instant::now();
    app.update(u);
    println!("{}", 1. / u.dt);
    println!("{:?}", std::time::Instant::now().duration_since(start));
}

So now it prints the framerate and the time it took to update. This is still butter smooth compared to just the update call.

120
353.351µs
120
335.323µs
120
295.962µs
120
295.962µs
120
198.009µs
120
207.023µs

Good. A consistent 120fps, what it’s supposed to be. What confuses me is how the increased “update” time (increased by the println!s) somehow makes the game run smooth while just the update makes it feel and run slow like as if it was calculating the first 100 primes every frame. :confused:
Any ideas?


Edit: Relevant deps:

[dependencies]
piston = "0.40.0"
piston2d-graphics = "0.29.0"
pistoncore-glutin_window = "0.52.0"
piston2d-opengl_graphics = "0.58.0"
1 Like