Why Processing sketches might be running slowly:


We all saw what happens to Processing sketches on slow computers. Here are some solutions that will get your sketches running faster.

Not Enough RAM

Processing is, by default, given 256MB RAM to work with. If you are experiencing lag, go to File > Preferences (Ctrl+comma) and check "Increase Maximum Available Memory", then enter a bigger value. I've only been able to give it 1024MB (1GB) RAM, but you might get more out of it, depending on your computer.

No Dual-Core Support

I don't believe that Processing supports multi-core processors by default. If you're doing something totally crazy, I strongly suggest you implement this.

Inefficient Code

Are you doing something like using loadImage() inside draw() or using get() a few thousand times? This will greatly slow down Processing. The Reference will tell you whether or not this will happen, and if there is a faster alternative. Commands like blend() and lerpColor() are by their nature processor-intensive, and them a lot will make problems happen. Additionally, always make sure you are doing something in the shortest amount of lines possible. Minimize the amount of code inside loops that will run many times. If you have println() giving you diagnostics on how things are working (something that is very handy for figuring out if a piece of code is actually running), comment it out and your app will fly.

Running Off External Memory

Reading files off a USB key will always take longer than off your hard drive. Always run Processing apps from a local directory in preference to any other method.

Creating Objects Instead of Changing Their Values

Making one color object at the beginning of the sketch, and then assigning it different values, is going to be a lot faster than making a new color object every time you need a color object. This is not a problem with lines that only run once, but if you're using nested loops then you might have this happen a thousand times per second, which bogs everything down.

Frame Rate

You can use the frameRate() property to adjust the frame rate of the sketch (how often draw() runs). The default is 60, but if the computer can't pull that you might be able to go as low as 12 without your sketch looking too jerky.

Sketch Size

It should be pretty obvious that working with an 800x600 image is a lot more difficult than with a 200x150 image, and working with a 4000x3000 image scaled down to size is more difficult still. Resize your images ahead of time and run them inside a smaller sketch. This should not be a problem for the end product since our sketches will likely be very small to fit into the web layout.

Unnecessary Looping

Is your sketch's final product a still image? Then you don't need to loop draw(). Either use noLoop() to stop code execution (you can use loop() to start it again if you need to, from a mouse click or key press) or simply do without setup() and draw() methods. However, if you do, then beware - you can't make any additional methods without setup() and draw().

Last Resort

If you really need to have your image update, but looping it blows up your computer, then do the following:
  1. Put noLoop() at the end of your draw(). This will stop the code from looping.
  2. Make a mousePressed() or keyPressed() method outside draw(). In it, put "loop();" (without quotes).
  3. Now, when you run the sketch, it will wait until your input before updating the screen. Make absolutely certain that the previous run of draw() has finished before you click, or java will crash and nobody is going to be happy.