How Can I Fix an Extremely Slow Sketch?

I am using an image in one of my sketches, and I am translating it. This appears to cause it to lag severely. My sketch also has classes, superclasses, and subclasses, as well as PVectors and keyboard interaction, which may contribute to the slowness. I am thinking that simplifying the sketch as much as I can will help it run faster. However, I do not know which parts of the code to simplify and how to simplify it, or if the code size is even part of the problem.

Answers

  • Extremely Slow Sketch

    I am using an image

    Where are you loading the image?

    Also, it is basically impossible for us to see what might be slowing down your sketch without seeing the code of the sketch.

  • However, I do not know which parts of the code to simplify and how to simplify it, or if the code size is even part of the problem.

    There is absolutely no correlation between "Code size" (presumably you mean the amount of code written) and performance; and little correlation between code size and 'simplicity' (in terms of maintenance). In fact longer code may perform better and will often be simpler in terms of interpretation.

    From the description of your sketch there are many possible factors that could affect performance - maybe you're loading images in draw()? - but without seeing your code we can't know where you should concentrate your efforts...

  • you should load images in setup and after use image load in draw so program is so fast. also you should use images with necesary size, not big images if you dont need it. you can use requestImage for load several images in time. I have a program that load 200 images in less 3 seconds and after draw is so fast. https://processing.org/reference/requestImage_.html

    por example if you are load images from website, program need so time(several seconds) for loadImage, is best save images in data folder or use a requestImage.

  • Your machine may be too slow to handle the image as well. I just ran into a similar problem like that with multiple windows slowing my machine way down because my graphics card has poor driver support for Windows 7. Once I moved the sketch to a machine that had proper driver support the lagging dissipated.

  • Here is the some of the code:

    PImage bg; void setup() { size(1000, 1000); noStroke(); rectMode(CENTER); bg = requestImage("bleuBoxInABlueWorldBackground.gif"); } void draw() { background(255); pushMatrix(); scale(10); translate(-bb.x/10, -bb.y/10); image(bg, 0, 0); scale(0.1); popMatrix(); }

    There are more objects created in setup(), but they are not images (just relatively simple square-like objects).

  • edited April 2016
    • Using only the code that you’ve posted, is it running slow? If it isn’t, then there isn’t enough code to find the problem;
    • Commenting parts of the code may be useful to find which ones are contributing to the slowness;
    • Your shapes may look simple, but if you’re drawing thousands of them it’s going to be slow. If you're drawing some shapes lots of times, storing them as PShapes may help: https://processing.org/tutorials/pshape/
    • Putting your transformations between pushMatrix and popMatrix is already resetting the matrix to the previous state, so scale(0.1); isn’t doing anything, you can remove it;
    • You’re scaling 10 times and them translating by something divided by 10, you could remove the division if you translate before your scale;
    • The more you scale, the slower the code will run, it may be better to use a bigger image.
  • I am not using many shapes at a time, and I have found that each additional transformation slows the sketch down. However, I think one or two transformations are essential to the sketch. Also, I cannot make the image too large to begin with (before scaling), otherwise the image file is too large to use and causes the sketch to run slowly.

  • Answer ✓

    If you post all the code you have and a link to somewhere we can download the image that you have, then maybe we can figure it out.

  • Thank you for your suggestions. I think I have found the problem, though.

  • That appears to be about Sketch, which is nothing to do with Processing.

    https://www.sketchapp.com/

Sign In or Register to comment.