IBeFrogs
IBeFrogs

Reputation: 3

How could having multiple images influence the speed of the app

The more images are currently in the sketch the slower image() runs which results in framerate lowering. What could be causing this?

//for each obstacle
for (int i=0; i<obsList.size(); i+=1){
ObstacleInstance = obsList.get(i);

  //if inside moving zone
  if (obsList.get(i).ObstacleX < 1920 + 400 && 0 - 400 
     obsList.get(i).ObstacleX) { 

      ObstacleInstance.move();

      //draws image in sketch; has performance issue
      image(ObstacleInstance.ObstacleImg, 
        ObstacleInstance.ObstacleX, 
        ObstacleInstance.ObstacleHeightFromTop,
        ObstacleInstance.ObstacleSizeX, 
        ObstacleInstance.ObstacleSizeY);
    }
  }
}

PS: used timer to see the time image() took to run and it increased with more images and decreased after removing them.

Upvotes: 0

Views: 42

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109567

One remark first: variables and fields should start with a small letter.

Every drawn image costs.

If the width and height of an object is not fixed (400), use something like:

final int visibleX = 0;
final int visibleY = 0;
final int visibleSizeX = 1920;
final int visibleSizeY = 1080;

for (Obstacle obs: obsList) {
    if (obs.ObstacleX < visibleX + visibleSizeX
            && obs.ObstacleX + obs.ObstacleSizeX > visibleX) { 
            && obs.ObstacleY < visibleY + visibleSizeY
            && obs.ObstacleY + obs.ObstacleSizeY > visibleY) { 
        ObstacleInstance.move();

        image(obs.ObstacleImg, 
            obs.ObstacleX, 
            obs.ObstacleHeightFromTop,
            obs.ObstacleSizeX, 
            obs.ObstacleSizeY);
    }
}

Overlapped drawing costs too, but is hard to optimize, other than with regions, collected areas (rectangles here).

Then the image call seems to be slow.

  • The image must not be drawn pixel by pixel.
  • The image must not be loaded from file every time.
  • Depending on the graphics implementation there you might hold the image in appropriate form (not packed bytes of a JPEG image).
  • There exists 2D transformations.

It seems you are working with some visualization library.

  • Look into affine transformaions like translate.
  • Implementation: System.arraycopy is faster than a for-loop.

To check the speed, it would be nice to draw rectangle outlines i.o. images. And move() should (hopefully) be substantial, not 0 or 1.

Upvotes: 1

Related Questions