How to improve the performance of Processing?

Recently I code the reaction-diffusion algorithm, following Daniel Shiffman’s tutorial. I use processing to code instead of P5.js in the tutorial.

When I run the sketch, I found the frame rate is so low when the sketch size is bigger than 1000×1000. And the renderer doesn’t help. CPU occupied about 12%.

I searched in the open processing. Many people used webgl2 to code, the perfomance is good, GPU is also nearly full occupied.

I haven’t learned P5.js and webgl2. So, is there any way to improve the performance of processing? Let GPU help calculating, or get more CPU power to work?

I don’t think processing is performance-optimized, because it is mainly a graphics library designed for ease of use. Does your sketch use a lot of memory? Is the code optimized? Also, I would try migrating your code to eclipse. For a 3d game in processing, I was averaging 40 fps with a very small project but eclipse gives a steady 60 fps for a much larger project.

1 Like

I am wondering how running a Processing sketch in the IDE is different than running it through an Eclipse project since both of them use the same libraries and PApplet under the hood :wink:

1 Like

@IpNeNaEcRe switching to Eclipse will not make your sketches run faster. It could however give you better tools to assess where the bottlenecks, are and more options to use alternatives.

If you are new to programming then it’s definitely a steep learning curve. I would recommend playing with other algorithms, other tutorials and see what you enjoy, and learn in the process.

5 Likes

@haschdl Yes, I’m a new learner. Thanks for advice.

1 Like

Having read some of the source code, I’d say Processing has ‘enough’ optimization. The rest is upto the person using it.

Perhaps the best I can do to contribute to this topic is provide some advice:

  • Please research about the concept of stack and heap memory allocations. The concept of memory allocation is used mostly by C and C++ programmers, but it is useful regardless of the language you use.

Mostly, it is about preventing declaring new objects - it is fine to write code such as ClassName objectName = someOtherObjectName;, but using the new keyword inside functions/methods that you or somebody else might call very often (such as Processing’s draw() function/method), is a consumer of performance. Please declare such variables in a namespace just outside such functions, where it is easier to read and change their values.

The JVM’s JIT-compiler might already be aware of such issues, but it is better for us to not cause such issues in the first place!

  • DSA classes can often give you better methods for doing certain things, use those.

  • For the case of using Processing specifically, you may want to look into utilities such as the PShape class, …and avoid heavy ones such as PGraphics.

  • Lastly, it is just a better choice to spend time learning to write optimized code whenever possible.

Spend time with making test projects to measure your code’s performance, and compare different methods. It is helpful to use the internet for knowledge on such matters. Many stackoverflow posts have made such matters clear.

I myself have the [bad] habit of thinking of possible optimizations in code before actually writing it, which is not considered good by the software industry (older members, such as those who write C and C++, or graphics applications like us, still consider this a very important skill). However, yes, it is important to spend time optimizing your code as soon as you’re done writing it, no matter you are using code to make art, or some heavy application (such as a game). This must be made into a habit.

Do not waste time thinking of every little optimization, though, the software industry, and legendary computer scientists such as Sir Donald Knuth (I hope that was the correct name!) have taught us not to spend too much time optimizing for every little thing; as Sir Donald Knuth said, a programmer, will only need to optimize so much, for only 3% of their entire career!

…also, in computer graphics, we often have situations we can do nothing about - needing seperate loops for updating different aspects of a list of objects, where you require all objects of a list to already have received updates, and situations where your program has graphics that need to be displayed on a screen that could change its size, and so you need to resize them according to the new dimensions of the screen (although techniques such as projection already do this for us, such situations can arrive sometimes) - in such cases, people who care about optimization prefer to make variables with such data cached and updated only when the screen size changes (imagine making a set of variables such as pwidth and pheight!). However, it is often not possible to maintain so many variables, and doing simple math with variables you might already have - such as those holding the coordinates of the center, is a bad idea, too, if the expression becomes complex, that is, since this makes the code harder to read for anybody not familiar with it, which, after you have finished your project, is also applicable to you!

I hope my advice helps, and wish for you to gain so much intellect, that you never need to worry about optimization ever again - thanks for reading through my ‘rant’! ":smiley:

4 Likes

I recently wrote a simple Game of Life sketch using Java 8 streams and the improved performance was phenominal. Java streams can make better use of multicore processors IF your algorithm can be solved using multiple independant processes. It is certainly something worth exploring.

3 Likes

PS Did you use the P2D or P3D renders?!

It makes little difference which renderer is used because the issue is the time to calculate the next generation. This video shows the Game of Life with 6 million grid cells and the calculation time for one generation varied between 4.3 and 6.9 milliseconds for all three renderers (JAVA2D, P2D and P3D). This was done on a 3.6GHz Intel Core i9 processor. Without parallel processing it was ~22 ms and you can see in the video the animation is very slow.

2 Likes

Oh! I realized it now… it’s the CPU side of things :sweat_smile:

(…what I also realized now is that you are Peter Lager 0_0)