How can I fix stuttering. Is it due to unoptimized code?

Hi, so I’m making a game in Processing but unfortunately, I can’t seem to get the program to run smoothly. Sometimes it will stutter while running, while the next time be completely fine even though it is running the same code the whole time and is quite minimal. I have already replace code so that objects are reused, but even just making a single line slowly draw along the screen by shifting x every frame causes stutters.

Not sure if I can upload a whole project here but I’m going to try:

Any help would be greatly appreciated!

I have tried multi-threading which didn’t seem to make it any faster ( thread() for calculating pipe’s positions separately in a while loop while draw() just draws the current state )

To play the game you press ‘S’ to go to the playing mode and press/hold space to actually make the game run

Couldn’t load it, no permission

sorry, just fixed permissions

1 Like

Hi @djx, You’re missing a } in keyPressed(). I ran it, pressed S and s, nothing happened. Guess at why it doesn’t run smoothly: Not completing all the code within the frame period. 1000/60 = 17 mS. (Think you can’t have 17mS only 10 or 20 depending on computer/O.S.)

Sorry, would you mind trying it again? I fixed that error but forgot to upload it. I also progressed more with the design. The lag is still present sometimes (seems to be if you try start the game too quickly) but it is better.

To play the game you press ‘S’ to go to the playing mode and press/hold space to actually make the game run :slight_smile:


I did not get to the stuttering… not get a quick response here to the “space” bar so could not play for any length of time.

This will show frameRate in title bar:

surface.setTitle("FrameRate: " + frameRate); //Set the frame title to the frame rate

From this example of an optimization:

I have not scrutinized the code.

That example may not apply in your case; it just illustrates that sometimes there is room for improvement.


1 Like

Hello again…

You can add some code to monitor these stutters:

I got carried away with above…

It can be as simple as

  • checking time between frames using millis()
  • if it is much greater than 1/60fps = 17 ms (rounded up to nearest int) display something using println() or flash screen colors.
  • println() every frame can also slows things down so only use this with an if()

If you just monitor frameRate there is some averaging:

Time between frames show the spikes.


I have the code running. Can see the stutter in the way the pipes move. Reduced frameRate to 30, removed the plane, top half of pipe, scores. All that’s left is the pipe x calc and the pipe redraw. It still stutters. It gives the appearance of skipping one draw. Hmm…


I can’t see any stutter on my computer.
Are you speaking of the left pipe when it exits the screen on the left?

As the pipes move across, every few seconds they move jumpily, as though 1 frame is missing. I’ve tried it with various frameRates. With 100 or 50 it moves in a constant but slightly rough way. With other values it stutters occasionally. It think the Processing timebase is beating with the PCs timeslicing (10 or 20 mS). Sometimes it runs smoothly for a couple of seconds - how does it do that and why can’t do that continuously? I’ve tried turning the priority up in task manager, didn’t help.

Hooo I get it now.

When you use frameRate(60), processing does its best to keep it constant but it is not guaranteed.

Instead what you should do is get the elapsed time between each frame and pass it as an argument to your “move” functions. Then instead of moving by a set amount, you can move depending on the elapsed time.

1 Like

Look at the example, Topics, Advanced Data, Create Shapes, PolygonPShapeOOP3. It has more moving items, and it’s perfectly smooth. What differences between that and Wings? Wings doesn’t have P2D and it uses images (maybe +others). Try putting P2D into Wings - stutters very occasionally.

See Quarks Place


No noticeable stutter with the P2D renderer.

See size() reference:

I monitored time between frames adding a bit of code:

Once it settled down it was stable.

I also tried integers only in pipe; it did not make a noticeable difference.