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

**URL:** <https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160>\
**Category:** Coding Questions\
**Created:** [September 9, 2021, 6:31am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160 "2021-09-09T06:31:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![DJx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/djx/32/14417_2.png) [@DJx](https://discourse.processing.org/u/DJx)\
**Post date:** [September 9, 2021, 6:31am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/1 "2021-09-09T06:31:46Z")

</div>

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:

> **[Wings.zip](https://drive.google.com/file/d/1HF_OYa3Wnw5sl5FAmi-gqTfiJpgu9tJQ/view?usp=sharing)**
>
> Google Drive file.

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 9, 2021, 7:24am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/2 "2021-09-09T07:24:21Z")

</div>

Couldn’t load it, no permission

---

<div class="post-metadata">

**Author:** ![DJx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/djx/32/14417_2.png) [@DJx](https://discourse.processing.org/u/DJx)\
**Post date:** [September 9, 2021, 7:24am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/3 "2021-09-09T07:24:42Z")

</div>

sorry, just fixed permissions

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [September 9, 2021, 9:21am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/4 "2021-09-09T09:21:49Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![DJx](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/djx/32/14417_2.png) [@DJx](https://discourse.processing.org/u/DJx)\
**Post date:** [September 9, 2021, 9:35am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/5 "2021-09-09T09:35:20Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 9, 2021, 10:22am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/6 "2021-09-09T10:22:03Z")

</div>

Hello,

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:

> [@Framerate going down unexpectedly](https://discourse.processing.org/t/framerate-going-down-unexpectedly/16400/2):
>
> Hello, I tried it with this: And modified: int noOfPoints = 250; void draw() { surface.setTitle("FrameRate: " + frameRate); //Set the frame title to the frame rate` public void show() { stroke(255, 50); strokeWeight(2); point(pos.x, pos.y); fill(155, 0, 0); float tmp2 = map(pos.x, 0, width, 5, 20); //println(pos.x); //println(frameRate); textSize(int(tmp2)); text("X", pos.x, pos.y); } I cast the textSize to an int and frame rates were 60 fps. …

I have not scrutinized the code.

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

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 9, 2021, 10:31am UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/7 "2021-09-09T10:31:57Z")

</div>

Hello again…

You can add some code to monitor these stutters:

> [@FrameRate hiccups](https://discourse.processing.org/t/framerate-hiccups/11947/15):
>
> I had some fun visualizing the data available to me. I displayed frameRate data (which averages) to framerate calculated from time between frames. [https://processing.org/reference/frameRate.html](https://processing.org/reference/frameRate.html) Code (you will have to select use “true” or “false” to see different modes of output): PrintWriter output; Table table; int time = 16\*60+40; //16s\*60f/s = 960f hiccups end after this time boolean dispMode = true; // "true" stores to file and plots after "time", "false" plots in real time lo…

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:

> **[frameRate / Reference](https://processing.org/reference/frameRate.html)**
>
> The system variable frameRate contains the approximate frame rate of the software as it executes. The initial value is 10 fps and is updated with each frame. The value is averaged (integrated) …

Time between frames show the spikes.

`:)`

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [September 9, 2021, 12:05pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/8 "2021-09-09T12:05:54Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 9, 2021, 4:36pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/9 "2021-09-09T16:36:06Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [September 9, 2021, 4:38pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/10 "2021-09-09T16:38:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [September 9, 2021, 4:41pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/11 "2021-09-09T16:41:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [September 9, 2021, 4:45pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/12 "2021-09-09T16:45:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 9, 2021, 7:53pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/13 "2021-09-09T19:53:09Z")

</div>

See [Quarks Place](http://www.lagers.org.uk/how-to-ps/ht-anim-01/index.html)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 9, 2021, 10:51pm UTC](https://discourse.processing.org/t/how-can-i-fix-stuttering-is-it-due-to-unoptimized-code/32160/14 "2021-09-09T22:51:32Z")

</div>

Hello,

No noticeable stutter with the P2D renderer.

See size() reference:

> **[Reference](https://processing.org/reference/size_.html)**
>
> Defines the dimension of the display window width and height in units of pixels. In a program that has the setup() function, the size() function must be the first line of code inside …

I monitored time between frames adding a bit of code:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f0881a0b4dc1ca9dcda88c3e82ce68f6418a97de.png)

Once it settled down it was stable.

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

`:)`
